• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

DOM and XPath Problem

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic