Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2005

Tip: Looking for answers? Try searching our database.

XPath on DOMTree

Thread view: 
Marvin_123456 - 12 Jul 2005 15:57 GMT
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
Mike Schilling - 27 Jul 2005 22:19 GMT
> Hello,
>
[quoted text clipped - 10 lines]
> 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.
Marvin_123456 - 28 Jul 2005 09:11 GMT
Mike Schilling schrieb:
> > Hello,
> >
[quoted text clipped - 17 lines]
> 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
Steve W. Jackson - 28 Jul 2005 18:29 GMT
> Mike Schilling schrieb:
> > > Hello,
[quoted text clipped - 33 lines]
> 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 =
Signature

Steve W. Jackson
Montgomery, Alabama

Mike Schilling - 28 Jul 2005 19:04 GMT
> Mike Schilling schrieb:
>> > Hello,
[quoted text clipped - 34 lines]
> 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."


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.