これはあなたができることのサンプルです:
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>
更新:孫を挿入するコードを追加しました。