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 / September 2006

Tip: Looking for answers? Try searching our database.

Java5 Xpath problem

Thread view: 
Moiristo - 25 Sep 2006 15:20 GMT
I am trying to evaluate an XML document using the javax.xml libraries,
but I guess I'm doing something wrong. I need to get all 'layer'
attributes from the document. To do this, I did this:

NodeList layers = (NodeList) path.evaluate("/root//layer",
    <xml inputsource>, XPathConstants.NODESET);

Then, I need to use the elements within that layer. The code I use is:

for(int i = 0;i<layers.getLength();i++){
     Node layer = layers.item(i);
     lname = layer.getNextSibling().getNodeValue().trim();
     lsource = layer.getNextSibling().getNodeValue().trim();
     ldest = layer.getNextSibling().getNodeValue().trim();
}

It finds the layers within the document, but when I store the element
values in the for-loop, it stores empty strings (when I call
getNodeName(), it returns "#text"). Do I need to evaluate the expression
differently, because it seems like the elements within the layers aren't
evaluated.

As an example, a layer node:   

        <layer>
            <name>
                Name
            </name>
            <sourceTable>
                Source
            </sourceTable>
            <destColumn>
                gcolName
            </destColumn>
    </layer>

TIA.
Oliver Wong - 25 Sep 2006 16:32 GMT
>I am trying to evaluate an XML document using the javax.xml libraries,

   You're actually evaluating an XPath on an XML document, not the XML
document itself.

> but I guess I'm doing something wrong. I need to get all 'layer'
> attributes from the document.

   "layer" is an element, not an attribute.

> To do this, I did this:
>
[quoted text clipped - 28 lines]
>             </destColumn>
> </layer>

   layer.getNextSibling(), which you call 3 times, will always return the
same node, and I don't think it's the node you want anyway. See
http://java.sun.com/j2se/1.5.0/docs/api/org/w3c/dom/Node.html#getChildNodes()

   - Oliver
Moiristo - 25 Sep 2006 17:26 GMT
>    You're actually evaluating an XPath on an XML document, not the XML
> document itself.
>    "layer" is an element, not an attribute.

You're right, sorry for my bad terminology :) I had a rough weekend..

>    layer.getNextSibling(), which you call 3 times, will always return
> the same node, and I don't think it's the node you want anyway. See
> http://java.sun.com/j2se/1.5.0/docs/api/org/w3c/dom/Node.html#getChildNodes()

Hmm, RTFM you say? You're right, I thought the method just advanced to
the next node when I called it. Fixed it now :) thnx!
Martin Honnen - 25 Sep 2006 17:37 GMT
> NodeList layers = (NodeList) path.evaluate("/root//layer",
>     <xml inputsource>, XPathConstants.NODESET);
[quoted text clipped - 4 lines]
>      Node layer = layers.item(i);
>      lname = layer.getNextSibling().getNodeValue().trim();

You can use e.g.
  lname = layer.getElementsByTagName("name").item(0);
Or you can use XPath again e.g.
  lname = (Node) path.evaluate("name", layer, XPathConstants.NODE);

Signature

    Martin Honnen
    http://JavaScript.FAQTs.com/



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



©2009 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.