[jdom-interest] query on JDOM 1.0 content replacement methods

Scott Yeadon scott.yeadon at anu.edu.au
Sun Sep 26 23:50:31 PDT 2004


Hi,

Can someone explain how the method chaining for replacing parts of 
documents works in JDOM 1.0. The code below doesn't work as the results 
returned from the XPath.selectNodes indicates the nodes may be clones 
and not part of the original document (since -1 is returned when trying 
to find the index of the nodeset returned from the XPath query). I would 
assume this is basic functionality so I must be doing something wrong - 
basically I need to find part of a document via an XPath expression and 
replace the result with new XML. The XPath results are correct based on 
a toString()-ing of the element returned.

SAXBuilder sb = new SAXBuilder();
Document profileDocument = sb.build("/mydocs/xml.txt");
XPath profileXPath = XPath.newInstance("//upload-profile[@name='" + 
profileName + "']");
List profileNode = profileXPath.selectNodes(profileDocument);
if (profileNode.size() > 0)
{
   ListIterator li = profileNode.listIterator();
   Element e = (Element)li.next()
   int i = profileDocument.indexOf(e);
   if (i == -1)
   {
       System.out.println("not a child, " + i);
    }
    else
   {
       profileDocument.removeContent(i);
       profileDocument.addContent(i,someContent);
       new XMLOutputter().output(profileDocument,new BufferedWriter(new 
FileWriter("/mydocs/xml2.txt", false)));
}
}

Thanks.

Scott.


More information about the jdom-interest mailing list