问题
Iam new to DOM parsing, their is a requirement of adding the elements at specific positions in the existing xml file using DOM parser. We will get the positions and element names which are to be added, from the properties file, as shown
abc.properties
3 = X
4 = Y
6 = Z
The output xml file after adding X,Xchild,Y and Z elements, is as shown
demo.xml
<root>
<A>text</A>
<B>text</B>
<X>
<Xchild>text</Xchild>
</X>
<Y>text</Y>
<C>text</C>
<Z>text</Z>
</root>
Please suggest me that how can we do it. Thanks in advance
回答1:
This is a sample of what you may do:
import java.io.IOException;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import javax.xml.parsers.*;
import javax.xml.xpath.*;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
public class AddElementFromProp {
public static void main(String[] args)
throws ParserConfigurationException, SAXException, IOException,
XPathExpressionException, TransformerException, TransformerConfigurationException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse("demo.xml");
//The XPath part.
XPathFactory xfactory = XPathFactory.newInstance();
XPath xpath = xfactory.newXPath();
Node result = (Node)xpath.evaluate("/root/*[position()='3']", doc, XPathConstants.NODE);
Element toInsert = doc.createElement("X");
result.getParentNode().insertBefore(toInsert, result);
//////////////////////////////////////////////////////
result = (Node)xpath.evaluate("/root/*[position()='4']", doc, XPathConstants.NODE);
toInsert = doc.createElement("Y");
Text txt = doc.createTextNode("text");
toInsert.appendChild(txt);
result.getParentNode().insertBefore(toInsert, result);
//////////////////////////////////////////////////////
result = (Node)xpath.evaluate("/root/*[position()='5']", doc, XPathConstants.NODE);
toInsert = doc.createElement("Z");
txt = doc.createTextNode("text");
toInsert.appendChild(txt);
result.getParentNode().appendChild(toInsert);
////////////////////////////////////////////////////////
result = (Node)xpath.evaluate("/root/X", doc, XPathConstants.NODE); //If you know the name of the node
//result = (Node)xpath.evaluate("/root/*[position()='3']", doc, XPathConstants.NODE);//If you know the position of the node
toInsert = doc.createElement("xchild");
txt = doc.createTextNode("text");
toInsert.appendChild(txt);
result.appendChild(toInsert);
////////////////////////////////////////////////////////
// Write out the final xml file
// Use a Transformer for output
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult _result = new StreamResult("demo1.xml");
transformer.transform(source, _result);
}
}
Note: This is far from a robust solution (just for your reference). There is a bunch of assumptions here. No check has been done to any extent - such as is there already an element at the specific position? Is it the position for the last element? etc.
Input:
<?xml version="1.0"?>
<root><A>text</A><B>text</B><C>text</C></root>
Output:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root><A>text</A><B>text</B><X><xchild>text</xchild></X><Y>text</Y><C>text</C><Z>text</Z>root>
Update: added code to insert grand children.
来源:https://stackoverflow.com/questions/10758565/can-we-add-the-elements-at-specific-positions-in-the-existing-xml-file-using-dom