怎样结构化xml文件到下列样式?

04-11-17 redlly

有如下xml文件:

<?xml version="1.0" encoding="GB2312"?>
<usecase>
    <usecasename>initialize</usecasename>
    <precondition>idle</precondition>
    <precondition>managefail</precondition>
    <precondition>connectfail</precondition>
    <scenario>
      <scenarioname>scn1</scenarioname>
      <scenarioprobability>1</scenarioprobability>
      <postcondition>initialized</postcondition>
      <critical>true</critical>
      <message>
        <messagename>IN</messagename>
        <sender>GCS</sender>
        <receiver>SCS</receiver>
        <time>t0</time>
        <timeconstraint>null</timeconstraint>
      </message>
      <message>
        <messagename>INA</messagename>
        <sender>SCS</sender>
        <receiver>GCS</receiver>
        <time>t1</time>
        <timeconstraint>null</timeconstraint>
      </message>
    </scenario>
</usecase>
<p>

当我用JDOM的addContent方法给usecase节点添加一个元素<precondition>newPrecondition</precondition>后,整个文件结构变成下列这种格式(注意黑体部分):

<?xml version="1.0" encoding="GB2312"?>
<usecase>
    <usecasename>initialize</usecasename>
    <precondition>idle</precondition>
    <precondition>managefail</precondition>
    <precondition>connectfail</precondition>
    <scenario>
      <scenarioname>scn1</scenarioname>
      <scenarioprobability>1</scenarioprobability>
      <postcondition>initialized</postcondition>
      <critical>true</critical>
      <message>
        <messagename>IN</messagename>
        <sender>GCS</sender>
        <receiver>SCS</receiver>
        <time>t0</time>
        <timeconstraint>null</timeconstraint>
      </message>
      <message>
        <messagename>INA</messagename>
        <sender>SCS</sender>
        <receiver>GCS</receiver>
        <time>t1</time>
        <timeconstraint>null</timeconstraint>
      </message>
    </scenario>
  <precondition>newPrecondition</precondition>
</usecase>
<p>

而我希望生成这种格式(注意黑体部分):

<?xml version="1.0" encoding="GB2312"?>
<usecase>
    <usecasename>initialize</usecasename>
    <precondition>idle</precondition>
    <precondition>managefail</precondition>
    <precondition>connectfail</precondition>
  <precondition>newPrecondition</precondition>
    <scenario>
      <scenarioname>scn1</scenarioname>
      <scenarioprobability>1</scenarioprobability>
      <postcondition>initialized</postcondition>
      <critical>true</critical>
      <message>
        <messagename>IN</messagename>
        <sender>GCS</sender>
        <receiver>SCS</receiver>
        <time>t0</time>
        <timeconstraint>null</timeconstraint>
      </message>
      <message>
        <messagename>INA</messagename>
        <sender>SCS</sender>
        <receiver>GCS</receiver>
        <time>t1</time>
        <timeconstraint>null</timeconstraint>
      </message>
    </scenario>
</usecase>
<p>

怎样才能让一个document中或者同一节点下名字相同的子节点连在一起。这样看起来整个文档的结构会更加清晰。

banq
2004-11-20 22:37

在Collection排序,全部重写一遍。