Hello all,
I am trying to use DOM and XPath to develop a method that can append XML tags. For example:
Source:
< A >
< B > value </ B>
</ A >
Target:
< A >
< B > value </ B >
< B > value </ B >
</ A >
So here is my method:
public class MyXMLClass {
// Fields:
Documemt
doc;
... // methods for initialize doc
public void appendXMLTag(
String parentXPath, String childXPath)
throws TransformerException{
Node root = doc.getDocumentElement();
Node parent = XPathAPI.selectSingleNode(root, parentXPath);
Node child = XPathAPI.selectSingleNode(root, childXPath);
Node newChild = child.cloneNode(true);
parent.appendChild(newChild);
}
public static void main(Sting args[]){
MyXMLClass
test = new MyXMLClass();
try {
... // call methods to initialzie doc
test.appendXMLTag("/A", "/A/B");
...
}
catch (Exception e) {
...
}
}
=======================
Now, the problem is when I use XPathAPI (see the code below) to access the appended node, I got null pointer exception.
...
Node root = doc.getDocumentElement();
Node node = XPathAPI.selectSingleNode(root,"/A/B[2]");
I guess my appendXMLTag method does not work probably. Do you know if there is a way to fix it?
Thank you very much.
Brian