在XML文件上动态添加行

时间:2017-11-08 14:07:26

标签: java xml

我需要以动态的方式在新的XML文件上添加一些行。

第一个test.xml:

<?xml version="1.0" encoding="UTF-8"?>
cns:Document xmlns:cns="smpcfg.xsd" DatabaseComment="Partial Export">
  <Status NAME="D1" StatusType="D1, Normally closed, Event">
    <Bypass>false</Bypass>
    <ExternalName>BRIGHTON.CB.3940.D1</ExternalName>
  </Status>
  <Status NAME="D2" StatusType="D2, Normally closed, Event">
    <Bypass>false</Bypass>
    <ExternalName>BRIGHTON.CB.3940.D2</ExternalName>
  </Status>
</cns:Document>

我要添加到每个<Status>元素的行:

<Control NAME="Trip">
  <DeviceName>TRIP</DeviceName>
  <Timeout>60</Timeout>
</Control>
<Control NAME="Close">
  <DeviceName>CLOSE</DeviceName>
  <Timeout>60</Timeout>
</Control>

和最终的XML,带有预期的结果:

<?xml version="1.0" encoding="UTF-8"?>
cns:Document xmlns:cns="smpcfg.xsd" DatabaseComment="Partial Export">
  <Status NAME="D1" StatusType="D1, Normally closed, Event">
    <Bypass>false</Bypass>
    <ExternalName>BRIGHTON.CB.3940.D1</ExternalName>
        <Control NAME="Trip">
            <DeviceName>TRIP</DeviceName>
            <Timeout>60</Timeout>
        </Control>
            <Control NAME="Close">
              <DeviceName>CLOSE</DeviceName>
              <Timeout>60</Timeout>
        </Control>
  </Status>
  <Status NAME="D2" StatusType="D2, Normally closed, Event">
    <Bypass>false</Bypass>
    <ExternalName>BRIGHTON.CB.3940.D2</ExternalName>
       <Control NAME="Trip">
          <DeviceName>TRIP</DeviceName>
            <Timeout>60</Timeout>
        </Control>
        <Control NAME="Close">
              <DeviceName>CLOSE</DeviceName>
              <Timeout>60</Timeout>
        </Control>
  </Status>
</cns:Document>

也就是说,我需要为每个“状态”添加这些行。

你能说出一个主意吗?用Java或任何语言。

0 个答案:

没有答案