1.This is my example file "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 is my java code that parses the file "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 this time, the nodes are saved to a new file "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 would like that, there were several copies of nodes in file output.xml. Everyone this node started after the last node:
<?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 can the easiest way to do this in Java? What loops used for this?