8 Replies Latest reply: Mar 10, 2007 10:16 AM by 807606 RSS

    Problem with XPath API

    807606
      hi all,

      I have the following problem here: I'm trying to query a XML document with the XPath API, as shown below. However the result is:

      null
      null

      DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
          domFactory.setNamespaceAware(true); // never forget this!
          DocumentBuilder builder = domFactory.newDocumentBuilder();
          Document doc = builder.parse("MiniGrp3.xml");
      
          XPathFactory factory = XPathFactory.newInstance();
          XPath xpath = factory.newXPath();
          XPathExpression expr 
           = xpath.compile("/MiniGrp3/DiscoveryRoutine/Provided");
      
          Object result = expr.evaluate(doc, XPathConstants.NODESET);
          NodeList nodes = (NodeList) result;
          for (int i = 0; i < nodes.getLength(); i++) {
              System.out.println(nodes.item(i).getNodeValue()); 
          }
      The document I'd like to query is:
      <MiniGrp3>
      <DiscoveryRoutine>
                     <Provided> blabla</Provided>
                     <Resultant> resultantBlaBla</Resultant>
      </DiscoveryRoutine>
      <DiscoveryRoutine>
                     <Provided> blabla2</Provided>
                     <Resultant>resultantBlabla2</Resultant>
      </DiscoveryRoutine>
      <CompositionRoutine>
                     <Provided>22222222</Provided>
                     <Resultant> 3333333333</Resultant>
      </CompositionRoutine>
      <CompositionRoutine>
                     <Provided>44444444444  </Provided>
                     <Resultant> 55555555555 </Resultant>
      </CompositionRoutine>
      
      </MiniGrp3>
      Obviously, I expect to get some text as output.
      Maybe I'm wrong with the query string??


      Thanks,
      Pesho
        • 1. Re: Problem with XPath API
          807606
          No, the XPath's fine. The problem is that Node.getNodeValue() is documented to return null when you call it on an Element. See http://java.sun.com/j2se/1.5.0/docs/api/org/w3c/dom/Node.html or http://java.sun.com/j2se/1.5.0/docs/api/org/w3c/dom/Element.html to work out what you should be calling.
          • 2. Re: Problem with XPath API
            807606
            Thanks,

            I should have used .getTextContent() instead!
            XPathExpression expr = xpath.compile(/definitions/message/parts/@name);
            ...
            NodeList nodes = (NodeList) result;
                for (int i = 0; i < nodes.getLength(); i++) {
                     System.out.println(nodes.item(i).getTextContent());
                }
            the file I'd like to parse looks like:
            <?xml version="1.0" encoding="utf-8"?>
            <definitions name="interopLab" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://tempuri.org/4s4c/1/3/wsdl/def/interopLab">
            <message name="checkOutLibraryBook_Request">
                    <part name="libraryName" type="xsd:string"/>
                    <part name="bookISBN" type="xsd:string"/>
            </message>
            <message name="checkOutLibraryBook_Response">
                    <part name="libraryName" type="xsd:string"/>
                    <part name="checkOutLibraryBookConfirmation" type="xsd:string"/>
            </message>
            <portType name="checkOutLibraryBook"></portType>
            </definitions>
            I just want to get the values of all the attributes "name"
            I guess this time the problem is in my xPath Query.

            Regards,
            Pesho
            • 3. Re: Problem with XPath API
              807606
              part rather than parts, but otherwise looks fine. What output does it give?
              • 4. Re: Problem with XPath API
                807606
                yep, you're right about the spelling error.

                However, now it gives the same output: 0

                I guess that the attribute value is not recognized as TextContent !?


                Cheers,
                Pesho
                • 5. Re: Problem with XPath API
                  807606
                  actually the output is empty.
                  0 was the size of a list where I put the expected results.

                  In fact, this XPath Expression (/definitions/message/part/@name) returns an empty NodeList!

                  /definitions/message/part also returns an empty NodeList
                  even if I put some text inbetween the tags <part>texttext</part>
                  isn't this strange?

                  Regards,
                  Pesho

                  Message was edited by:
                  Pesho_318i
                  • 6. Re: Problem with XPath API
                    807606
                    .... and when I remove the attributes after <definitions ....> it works as expected. Obviously the attributes of "definitions" cause some problem...

                    do you have any idea how I can fix it?
                    • 7. Re: Problem with XPath API
                      807606
                      I suppose it might be a namespace thing. I've never used them, so I don't know how they work.
                      • 8. Re: Problem with XPath API
                        807606
                        PERFECT ! ! !

                        you helped so much :)

                        I just had to change a code fragment, dealing with namespace awareness.


                        It's now fine,
                        Cheers,
                        Pesho