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

这是我的示例文件“example.xml”

 7.0 FIN  ED XXX 2015-05-16 90 KIA  

2.这是我解析文件“example.xml”的java代码:

  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("" + ((Node) dispmarke.item(0)).getNodeValue() + ""); String dispbrd = prop.getProperty("prop4"); ((Node) dispmarke.item(0)).setNodeValue(dispbrd); System.out.println("" + ((Node) dispmarke.item(0)).getNodeValue() + ""); 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("" + ((Node) mod.item(0)).getNodeValue() + ""); prop.put("norm", "X300"); ((Node) mod.item(0)).setNodeValue(prop.getProperty("norm")); System.out.println("" + ((Node) mod.item(0)).getNodeValue() + ""); } } } 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.此时,节点将保存到新文件“output.xml”:

    7.0 FIN  ED XXX 2015-05-16 90 KIA   

4.我希望如此,文件output.xml中有几个节点副本。 此节点的每个人都在最后一个节点之后启动:

   7.0 FIN  ED XXX 2015-05-16 90 KIA    7.0 FIN  ED XXX 2015-05-16 90 KIA   ... 

5.如何用Java最简单的方法呢? 用于此的循环是什么?

我必须修改输入XML以获得根元素:

   7.0 FIN  ED XXX 2015-05-16 90 KIA   

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

  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 nodes); }; // Projection for the output document public interface OutputProjection { @XBWrite("/ROOT/*") void setNodes(List 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 nodes =projector.io().url("res://example.xml").evalXPath("/ROOT/*").asListOf(DOMAccess.class); // Create some copies List newNodeList = new LinkedList(); 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); } } 

该程序打印出来:

   7.0 FIN  ED XXX 2015-05-16 90 KIA    7.0 FIN  ED XXX 2015-05-16 90 KIA