I have been trying to understand XPath by reading the Javadocs.  The code
below is basically my understanding of how you would get a text node value
from a org.w3c.dom.Document and a DTM object using XPath syntax without
using XPathAPI.  However, I'm doing something wrong because DTM's are
supposed to be immutable yet when I change the value of the text node it is
also change on the DTM (or so I think).

snippet of code
=======================
Source domSource = new DOMSource(_doc.getDocumentElement()); //_doc is a
org.w3c.dom.Document already defined.
XPathContext _xpathcont = new XPathContext(); //not sure what the
XPathContext really does or if I'm using correctly
DTM dtm = _xpathcont.getDTM(domSource, false, null, false, false);

//Precompiling XPath object
XPath xpath = new XPath(xpathexpression, null, null, XPath.SELECT);
//xpathexpression is a String already defined

//Executing XPath object
XObject obj = xpath.execute(_xpathcont, _doc.getDocumentElement(), null);
// don't know if i'm getting XObject form DOM or a DTM built on the fly
NodeList nodelist  = obj.nodelist();

Node node = nodelist.item(0);
Node elem = node.getFirstChild().getFirstChild();  //quick n dirty, just
trying to get text node :)
System.out.println("Nodetype=" + elem.getNodeType());

System.out.println("XObject=" + obj.str()); //printing original/current
node

System.out.println("Node " + elem.getNodeName() + "=" + elem.getNodeValue
()); //printing original/current value

//modify the value of the text node
elem.setNodeValue("modified"); //modify text element/node value, this
should only affect the DOM (Document) object not DTM.

//attempting to get original value using DTM since they are supposed to be
immutable (is this how u get values from DTM's??)
obj = xpath.execute(_xpathcont, dtm.getDocument(), null);

//print entire node retrieved from DTM
System.out.println("XObject=" + obj.str());  //this prints out the new
value "modified" - i was expecting the old value

//print node value from modified node
System.out.println("Node " + elem.getNodeName() + "=" + elem.getNodeValue
()); //this prints out new value "modified", which is correct
========================

I'm probably doing this completely wrong, but this is what I have been able
to come up with by reading the Javadocs.
If anyone has an example of what I'm tyring to do, I would appreciate if
you send to me.  Thanks.

Reply via email to