For Programmers: Free Programming Magazines  


Home > Archive > Java Help > October 2004 > [HELP] Altering XML Document









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author [HELP] Altering XML Document
Gerorge Reece-Howe

2004-10-07, 4:00 am

Hi,

If I have an XML Document
<?xml version="1.0" encoding="UTF-8" ?>
<root>
<node1 />
....
<nodex nxattr="42" ><![CDATA[My Old Data]]></nodex>
....
<noden />
</root>

I am able to retrieve a given nodex text and attribute values by using JAXEN
XPath, but how do I edit the nxattr value and nodex text?


Thanks

Gerorge


Gerorge Reece-Howe

2004-10-07, 8:57 pm

> If I have an XML Document
> <?xml version="1.0" encoding="UTF-8" ?>
> <root>
> <node1 />
> ...
> <nodex nxattr="42" ><![CDATA[My Old Data]]></nodex>
> ...
> <noden />
> </root>
>
> I am able to retrieve a given nodex text and attribute values by using

JAXEN
> XPath, but how do I edit the nxattr value and nodex text?


I got this working eventually.
try{
XPath expression = new DOMXPath("//root/nodex");
result=(Element) expression.selectSingleNode(xmlDocument);

//This worked from the outset
result.setAttribute("nxattr", "newatrrvalue");

//This didn't work. The output showed the node with an updated attribute
value, but the text unchanged
//result.setNodeValue("My New Data");

//This, however, worked just fine
result.getFirstChild.setNodeValue("My New Data");

System.out.println(result.toString());
}
catch(JaxenException e){e.printStackTrace;}
catch(DOMException e){e.printStackTrace;}


So when selecting a node, it appears that the Nodes text value is a child of
the node, although the attribute is not.

Gerorge


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com