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

Scott Yeadon scott.yeadon at anu.edu.au
Wed Sep 29 15:23:34 PDT 2004


Per,

Thanks. That worked fine, only additional thing was to cast p to Element 
for using the setContent method.

Scott.

Per Norrman wrote:

> 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