在同一输出 xml 文件中复制节点 -java

Copy nodes in the same output xml file -java

1.This 是我的示例文件 "example.xml"

<?xml version="1.0" encoding="UTF-8"?>
  <VER>7.0</VER>
    <NATIONALITY>FIN</NATIONALITY>
    <DATA>
    <USER>ED</USER>
    <PLZ>XXX</PLZ>
    <BEGIN>2015-05-16</BEGIN>
    <CURRENCY>90</CURRENCY>
    <MARKE>KIA</MARKE>
   <DATA>

2.This 是我的 java 解析文件的代码 "example.xml" :

 public static void main(String argv[]) {
try {
   Properties prop = System.getProperties();
   File file = new File("E:/workspace/example.xml");
   DocumentBuilderFactory dbildfactory =DocumentBuilderFactory.newInstance();
   DocumentBuilder dbild = dbildfactory.newDocumentBuilder();
   Document doc = dbild.parse(file);
   doc.getDocumentElement().normalize();
   NodeList nodeL = doc.getElementsByTagName("ALL");

   for (int s = 0; s < nodeL.getLength(); s++) {
   Node nodde = nodeL.item(s);

    if (nodde.getNodeType() == Node.ELEMENT_NODE){ 

    Element fstElmnt = (Element) nodde;

    NodeList list = fstElmnt.getElementsByTagName("MARKE");
    Element disp = (Element) list.item(0);
    NodeList dispmarke = disp.getChildNodes();
    System.out.println("<MARKE>" + ((Node)               

    dispmarke.item(0)).getNodeValue() + "</MARKE>");
    String dispbrd = prop.getProperty("prop4");
    ((Node) dispmarke.item(0)).setNodeValue(dispbrd);
    System.out.println("<MARKE>" + ((Node)     

    dispmarke.item(0)).getNodeValue() + "</MARKE>");

    if(dispmarke.item(0).getNodeValue().equals("LEX") ){

      NodeList nod = fstElmnt.getElementsByTagName("MARKE");
      Element element = (Element) nod.item(0);
      NodeList mod = element.getChildNodes();
      System.out.println("<MARKE>" + ((Node) mod.item(0)).getNodeValue() + "</MARKE>");
      prop.put("norm", "X300");
      ((Node) mod.item(0)).setNodeValue(prop.getProperty("norm"));
      System.out.println("<MARKE>" + ((Node) mod.item(0)).getNodeValue() + "</MARKE>"); 
      }
    }

}

      TransformerFactory transformerFactory = 
      TransformerFactory.newInstance();
      Transformer transformer = 
      transformerFactory.newTransformer();
      transformer.setOutputProperty(OutputKeys.INDENT, "yes");
      DOMSource source = new DOMSource(doc);
      StreamResult console = new StreamResult(System.out);
      StreamResult fil = new StreamResult(new File("E:/workspace/res/output.xml"));
      transformer.transform(source, console);
      transformer.transform(source, fil);
      }

      catch (Exception e) {
      e.printStackTrace();
      }
     }

3.At这次节点保存到新文件"output.xml":

 <?xml version="1.0" encoding="UTF-8"?>
 <ALL>
  <VER>7.0</VER>
    <NATIONALITY>FIN</NATIONALITY>
    <DATA>
    <USER>ED</USER>
    <PLZ>XXX</PLZ>
    <BEGIN>2015-05-16</BEGIN>
    <CURRENCY>90</CURRENCY>
    <MARKE>KIA</MARKE>
   <DATA>
  </ALL>

4.I 会喜欢,文件 output.xml 中有多个节点副本。 大家这个节点在最后一个节点之后开始:

<?xml version="1.0" encoding="UTF-8"?>

  <ALL>
    <VER>7.0</VER>
     <NATIONALITY>FIN</NATIONALITY>
      <DATA>
      <USER>ED</USER>
      <PLZ>XXX</PLZ>
      <BEGIN>2015-05-16</BEGIN>
       <CURRENCY>90</CURRENCY>
       <MARKE>KIA</MARKE>
       <DATA>
     </ALL> 
   <ALL>
     <VER>7.0</VER>
      <NATIONALITY>FIN</NATIONALITY>
      <DATA>
        <USER>ED</USER>
        <PLZ>XXX</PLZ>
        <BEGIN>2015-05-16</BEGIN>
        <CURRENCY>90</CURRENCY>
        <MARKE>KIA</MARKE>
        <DATA>
      </ALL>
  ...

5.How 最简单的方法是 Java 吗?为此使用了哪些循环?

我必须修复您的输入 XML 才能拥有根元素:

<?xml version="1.0" encoding="UTF-8"?>
<ROOT>
  <VER>7.0</VER>
    <NATIONALITY>FIN</NATIONALITY>
    <DATA>
    <USER>ED</USER>
    <PLZ>XXX</PLZ>
    <BEGIN>2015-05-16</BEGIN>
    <CURRENCY>90</CURRENCY>
    <MARKE>KIA</MARKE>
   </DATA>
</ROOT>   

然后我可以使用 XMLBeam(一个数据投影库,披露:我隶属于这个项目)

  1. 读取输入,
  2. 将其包装成一个 ALL 元素
  3. 复制输入并
  4. 打印出来

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import org.xmlbeam.XBProjector;
import org.xmlbeam.XBProjector.Flags;
import org.xmlbeam.annotation.XBWrite;
import org.xmlbeam.dom.DOMAccess;

@SuppressWarnings("javadoc")
public class DuplicateNode {

    // Projection to embed some nodes into an "ALL" element
    public interface AllNodes {
        @XBWrite("./*")
        AllNodes setNodes(List<DOMAccess> nodes);
    };

    // Projection for the output document
    public interface OutputProjection {
        @XBWrite("/ROOT/*")
        void setNodes(List<AllNodes> nodes);
    };

    public static void main(String[] args) throws IOException {
        // Create the projector we will use to project the data
        XBProjector projector = new XBProjector(Flags.TO_STRING_RENDERS_XML);

        //Read all nodes from input document
        List<DOMAccess> nodes =projector.io().url("res://example.xml").evalXPath("/ROOT/*").asListOf(DOMAccess.class);

        // Create some copies
        List<AllNodes> newNodeList = new LinkedList<AllNodes>();
        newNodeList.add(projector.projectEmptyElement("ALL",AllNodes.class).setNodes(nodes));
        newNodeList.add(projector.projectEmptyElement("ALL",AllNodes.class).setNodes(nodes));
        //...

        // Create an output projection that will take the copied nodes
        OutputProjection outputProjection = projector.projectEmptyDocument(OutputProjection.class);

        // set nodes to the output projection
        outputProjection.setNodes(newNodeList);

        // print it out (or write to file, stream,...)
        System.out.println(outputProjection);
    }
}

这个程序打印出来:

<ROOT>
  <ALL>
    <VER>7.0</VER>
    <NATIONALITY>FIN</NATIONALITY>
    <DATA>
    <USER>ED</USER>
    <PLZ>XXX</PLZ>
    <BEGIN>2015-05-16</BEGIN>
    <CURRENCY>90</CURRENCY>
    <MARKE>KIA</MARKE>
   </DATA>
  </ALL>
  <ALL>
    <VER>7.0</VER>
    <NATIONALITY>FIN</NATIONALITY>
    <DATA>
    <USER>ED</USER>
    <PLZ>XXX</PLZ>
    <BEGIN>2015-05-16</BEGIN>
    <CURRENCY>90</CURRENCY>
    <MARKE>KIA</MARKE>
   </DATA>
  </ALL>
</ROOT>