怎樣結構化xml檔案到下列樣式?

redlly發表於2004-11-17
有如下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 class="indent">

當我用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 class="indent">

而我希望生成這種格式(注意黑體部分):

<?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 class="indent">

怎樣才能讓一個document中或者同一節點下名字相同的子節點連在一起。這樣看起來整個文件的結構會更加清晰。

相關文章