Results 1 to 6 of 6
  1. #1
    baiguai is offline Member
    Join Date
    Jun 2010
    Posts
    6
    Rep Power
    0

    Default Getting Child Nodes With XPath.evaluate on an Element as Opposed to a Node

    I am trying to get an Element's child nodes using XPath, but I keep getting a 'cannot find symbol' error where I call XPath.evaluate. I think the trouble is I am passing in an Element not a Node (the examples I've seen online pass in a Node) - but I need to use the methods available to the Element object elsewhere so I don't have a Node object to pass in. Here is the code in question:

    Java Code:
        public static NodeList GetChildElements(Element parent, String xpath)
        {
            XPathFactory factory = XPathFactory.newInstance();
            XPath xp = factory.newXPath();
            NodeList nodes = null;
            
            nodes = (NodeList) xpath.evaluate(xpath, parent, XPathConstants.NODESET);
    
            return nodes;
        }
    How would I go about getting a NodeList from an Element using xpath? If I need to pass in a Node, how would I convert my Element to a Node?

    Thank you in advance!

  2. #2
    baiguai is offline Member
    Join Date
    Jun 2010
    Posts
    6
    Rep Power
    0

    Default Re: Getting Child Nodes With XPath.evaluate on an Element as Opposed to a Node

    Eeeeesh this is driving me NUTS. I've gone through and modified my code so I can pass in a Node instead of an Element - I'm still getting the error:

    cannot find symbol
    symbol : method evaluate(org.w3c.dom.Document,org.w3c.dom.Node,jav ax.xml.namespace.QName)
    location: interface javax.xml.xpath.XPath
    return (NodeList)xPath.evaluate(xmlDocument, node, XPathConstants.NODESET);

    All the mentioned classes are being imported so I'm not sure what the trouble is.

  3. #3
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default Re: Getting Child Nodes With XPath.evaluate on an Element as Opposed to a Node

    Quote Originally Posted by baiguai View Post
    I am trying to get an Element's child nodes using XPath, but I keep getting a 'cannot find symbol' error where I call XPath.evaluate.
    You are not calling XPath.evaluate()

  4. #4
    baiguai is offline Member
    Join Date
    Jun 2010
    Posts
    6
    Rep Power
    0

    Default Re: Getting Child Nodes With XPath.evaluate on an Element as Opposed to a Node

    Oh! I don't call evaluate() on my XPath instance? I'll try calling it on XPath, see if that works. All the examples I've seen so far call evaluate() on an instance of XPath (XPath: evaluate(String expression, Object item) : XPath**javax.xml.xpath**Java by API).

    Thanks!

  5. #5
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,058
    Rep Power
    5

    Default Re: Getting Child Nodes With XPath.evaluate on an Element as Opposed to a Node

    The trouble is that you're trying to pass a Document instead of a String.
    Get in the habit of using standard Java naming conventions!

  6. #6
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default Re: Getting Child Nodes With XPath.evaluate on an Element as Opposed to a Node

    Post your import statements.

Similar Threads

  1. Replies: 6
    Last Post: 07-22-2011, 02:05 PM
  2. Replies: 1
    Last Post: 07-14-2010, 08:58 AM
  3. Get selected Node Value of a child element
    By Ms.Ranjan in forum XML
    Replies: 6
    Last Post: 10-06-2009, 09:31 PM
  4. How to add different menuitems to child node
    By sudhakar.cheru in forum AWT / Swing
    Replies: 2
    Last Post: 09-16-2009, 06:32 AM
  5. How to get value of specific child node
    By sito42 in forum New To Java
    Replies: 1
    Last Post: 07-13-2009, 12:00 PM

Tags for this Thread

Posting Permissions

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