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

Per Norrman per.norrman at austers.se
Mon Sep 27 03:33:22 PDT 2004


Hi,

your xpath expression selects certain upload-profile elements
at any level, but you test each selected element for being
a child of the document (which, btw, can have only one child
element).

You probably want to get the parent of each selected element
and replace its content.

> 
> 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()
     Parent p = e.getParent();
     int i = p.indexOf(e);
>   if (i == -1)
>   {
>       System.out.println("not a child, " + i);
>    }
>    else
>   {
         p.setContent(i, someContent);
>       new XMLOutputter().output(profileDocument,new BufferedWriter(new 
> FileWriter("/mydocs/xml2.txt", false)));
> }

/pmn


More information about the jdom-interest mailing list