Results 1 to 7 of 7
  1. #1
    omripe is offline Member
    Join Date
    Jul 2011
    Posts
    13
    Rep Power
    0

    Default Is it possible to use xpath with a Node or NodeList objects?

    Hi!
    I have a problem which I need to solve.
    I have an XML file which I partially parse at one step of my program(For example getting all the user accounts with xpath into a node list)
    and then at a later phase of my program I need to get deeper into each user accound and check it's password, etc...(In addition it is problematic getting the username - for example - directly from the root node, because some of the intermediate tags are inserted by the user and they support wild card, for example jjj*fff is a match for the tag <jjjdsdsdfff> )

    The thing is, I dont see how I can use xpath with this sub tree..The only thing I see is using xpath all over again with the full XML document or navigating with the DOM of each account which is very tiresome and not efficient..
    What do you people think?
    Is there a solution to this?
    Thanks!! :)

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    Sure! If you write as example "/.../.." you select from the root node, but If you write "./.../" you select from the current node (in your example from the current account node!)!
    The point is standing for the current node!

  3. #3
    omripe is offline Member
    Join Date
    Jul 2011
    Posts
    13
    Rep Power
    0

    Default

    I am not sure that I understand. look at this example:
    Let's assume I have the following xml file:
    "<accounts>
    <account>
    <username></username>
    <password></password>
    </account>
    <account>
    <username></username>
    <password></password>
    </account>
    </accounts>"

    Let's assume that I use "xpath.Evaluate" with the xml document object and the path:"accounts/account"
    Now, I have a node list with 2 account nodes.
    (there is a reason I am doing it seperatly, the application demands it due to some complications in the SRS)

    Now, I want to use xpath only with the first account, meaning, I want to get - for example - to the user name without using the xml document object but only with the object nodeList.item(0).
    Is this possible?

  4. #4
    omripe is offline Member
    Join Date
    Jul 2011
    Posts
    13
    Rep Power
    0

    Default

    Can you show your point with the XML document I gave as an example?

  5. #5
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    Which library do you use? javax.xml and org.w3c ? Or JDOM or another XML-library ?

    javax.xml example:
    Java Code:
    		XPathExpression username = XPathFactory.newInstance().newXPath().compile("./username/text()");   //a new xpath expression 
    		Node usernameNode = (Node) username.evaluate(nodeList.item(0), XPathConstants.NODE);  // do not use the document here in the evaluate method - use the list item
    		System.out.println(usernameNode.getNodeValue());

  6. #6
    omripe is offline Member
    Join Date
    Jul 2011
    Posts
    13
    Rep Power
    0

    Default

    I use javax.xml and org.w3c.
    I going to try it now!
    Hope it works for me, thanks :)

  7. #7
    omripe is offline Member
    Join Date
    Jul 2011
    Posts
    13
    Rep Power
    0

Similar Threads

  1. How to get NodeList from XML file
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-13-2007, 09:16 AM
  2. xpath question
    By Ed in forum XML
    Replies: 1
    Last Post: 07-09-2007, 12:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •