这是您可以执行的操作的示例:
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);
}
}
注意:这远非一个强大的解决方案(仅供您参考)。这里有一堆假设。没有进行任何程度的检查 - 例如特定位置是否已经存在元素?它是最后一个元素的位置吗?等等
输入:
<?xml version="1.0"?>
<root><A>text</A><B>text</B><C>text</C></root>
输出:
<?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>
更新:添加了插入孙子的代码。