XPath on DOMTree

M

Marvin_123456

Hello,

I execute an XPath expression (with the javax.xml.xpath package) on a
DOM tree and get the result in a NodeList.

NodeList nodeList = (NodeList)xpath.evaluate("/a/b", document,
XPathConstants.NODESET);

The Node objects in the nodeList result are references to the nodes in
the DOM tree (document). So, when I change a value in a tree node, it
is also changed in the result and vice versa.

Is this behaviour consistent and one can rely on it that the result
consists always only of references to the source document DOM tree?

Thank you very much!
Regards,
Marvin
 
M

Mike Schilling

Marvin_123456 said:
Hello,

I execute an XPath expression (with the javax.xml.xpath package) on a
DOM tree and get the result in a NodeList.

NodeList nodeList = (NodeList)xpath.evaluate("/a/b", document,
XPathConstants.NODESET);

The Node objects in the nodeList result are references to the nodes in
the DOM tree (document). So, when I change a value in a tree node, it
is also changed in the result and vice versa.

Is this behaviour consistent and one can rely on it that the result
consists always only of references to the source document DOM tree?

That's an interesting question. Certainly the Nodes returned are full DOM
nodes; you can not only look at their immediate contents (e.g. their name)
but you can navigate from them up and down the DOM tree. In practice, the
efficient way to do this is to return the actual nodes, but an
implementation that copies the entire DOM and returns nodes from that copy
would be, as far as I know, conformant.
 
M

Marvin_123456

Mike said:
That's an interesting question. Certainly the Nodes returned are full DOM
nodes; you can not only look at their immediate contents (e.g. their name)
but you can navigate from them up and down the DOM tree. In practice, the
efficient way to do this is to return the actual nodes, but an
implementation that copies the entire DOM and returns nodes from that copy
would be, as far as I know, conformant.


Hi Mike,

thank you very much for your answer. What I am doing right now is to
query for a set of nodes with an XPath expression, modify the data in
the result nodes and then reuse the original DOM on that the query was
evaluated. This would not work, if only copies of the nodes would be
returned as query result, since the data modification on the result
would be lost in the original DOM. At the moment it seems that this
always works, so the original DOM nodes and no copies are returned. My
question was, whether this behaviour is specified (for the used
java-package) or just "by chance" and there may be exceptions of it.

Regards,
Marvin_123456
 
S

Steve W. Jackson

Marvin_123456 said:
Hi Mike,

thank you very much for your answer. What I am doing right now is to
query for a set of nodes with an XPath expression, modify the data in
the result nodes and then reuse the original DOM on that the query was
evaluated. This would not work, if only copies of the nodes would be
returned as query result, since the data modification on the result
would be lost in the original DOM. At the moment it seems that this
always works, so the original DOM nodes and no copies are returned. My
question was, whether this behaviour is specified (for the used
java-package) or just "by chance" and there may be exceptions of it.

Regards,
Marvin_123456

If you're using NodeList as provided by Sun's Java, you should read the
API JavaDocs on the NodeList interface. There, it specifically states
"NodeList objects in the DOM are live." So I personally would shy from
any implementation that violates that by implementing NodeList with
copies of the nodes in question.

= Steve =
 
M

Mike Schilling

Marvin_123456 said:
Hi Mike,

thank you very much for your answer. What I am doing right now is to
query for a set of nodes with an XPath expression, modify the data in
the result nodes and then reuse the original DOM on that the query was
evaluated. This would not work, if only copies of the nodes would be
returned as query result, since the data modification on the result
would be lost in the original DOM. At the moment it seems that this
always works, so the original DOM nodes and no copies are returned. My
question was, whether this behaviour is specified (for the used
java-package) or just "by chance" and there may be exceptions of it.

Yes, and my answer is "It'll work in the XPath implementation you're using,
but there's AFAIK no guarantee it will work with other ones."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top