Page 1 of 2 12 LastLast
Results 1 to 20 of 24
Like Tree1Likes

Thread: Specific attributes in XPath

  1. #1
    Eleeist is offline Member
    Join Date
    May 2011
    Posts
    84
    Rep Power
    0

    Default Specific attributes in XPath

    Hi,

    I am trying to select a element which attribute "name" has a value "test". However, there is an awful error showing up. Anyone willing to help?

    Example XML:

    Java Code:
    <row id="1" name="test" />
    My code so far:

    Java Code:
    /row/@name='test'
    The error:

    Java Code:
    Exception in thread "main" javax.xml.transform.TransformerException: A location path was expected, but the following token was encountered:  ]
        at com.sun.org.apache.xpath.internal.compiler.XPathParser.error(XPathParser.java:608)
        at com.sun.org.apache.xpath.internal.compiler.XPathParser.LocationPath(XPathParser.java:1599)
        at com.sun.org.apache.xpath.internal.compiler.XPathParser.PathExpr(XPathParser.java:1315)
        at com.sun.org.apache.xpath.internal.compiler.XPathParser.UnionExpr(XPathParser.java:1234)
        at com.sun.org.apache.xpath.internal.compiler.XPathParser.UnaryExpr(XPathParser.java:1140)
        at com.sun.org.apache.xpath.internal.compiler.XPathParser.MultiplicativeExpr(XPathParser.java:1061)
        at com.sun.org.apache.xpath.internal.compiler.XPathParser.AdditiveExpr(XPathParser.java:1003)
        at com.sun.org.apache.xpath.internal.compiler.XPathParser.RelationalExpr(XPathParser.java:928)
        at com.sun.org.apache.xpath.internal.compiler.XPathParser.EqualityExpr(XPathParser.java:868)
        at com.sun.org.apache.xpath.internal.compiler.XPathParser.AndExpr(XPathParser.java:832)
        at com.sun.org.apache.xpath.internal.compiler.XPathParser.OrExpr(XPathParser.java:805)
        at com.sun.org.apache.xpath.internal.compiler.XPathParser.Expr(XPathParser.java:788)
        at com.sun.org.apache.xpath.internal.compiler.XPathParser.PredicateExpr(XPathParser.java:1952)
        at com.sun.org.apache.xpath.internal.compiler.XPathParser.Predicate(XPathParser.java:1934)
        at com.sun.org.apache.xpath.internal.compiler.XPathParser.Step(XPathParser.java:1724)
        at com.sun.org.apache.xpath.internal.compiler.XPathParser.RelativeLocationPath(XPathParser.java:1633)
        at com.sun.org.apache.xpath.internal.compiler.XPathParser.LocationPath(XPathParser.java:1595)
        at com.sun.org.apache.xpath.internal.compiler.XPathParser.PathExpr(XPathParser.java:1315)
        at com.sun.org.apache.xpath.internal.compiler.XPathParser.UnionExpr(XPathParser.java:1234)
        at com.sun.org.apache.xpath.internal.compiler.XPathParser.UnaryExpr(XPathParser.java:1140)
        at com.sun.org.apache.xpath.internal.compiler.XPathParser.MultiplicativeExpr(XPathParser.java:1061)
        at com.sun.org.apache.xpath.internal.compiler.XPathParser.AdditiveExpr(XPathParser.java:1003)
        at com.sun.org.apache.xpath.internal.compiler.XPathParser.RelationalExpr(XPathParser.java:928)
        at com.sun.org.apache.xpath.internal.compiler.XPathParser.EqualityExpr(XPathParser.java:868)
        at com.sun.org.apache.xpath.internal.compiler.XPathParser.AndExpr(XPathParser.java:832)
        at com.sun.org.apache.xpath.internal.compiler.XPathParser.OrExpr(XPathParser.java:805)
        at com.sun.org.apache.xpath.internal.compiler.XPathParser.Expr(XPathParser.java:788)
        at com.sun.org.apache.xpath.internal.compiler.XPathParser.initXPath(XPathParser.java:127)
        at com.sun.org.apache.xpath.internal.XPath.<init>(XPath.java:176)
        at com.sun.org.apache.xpath.internal.XPath.<init>(XPath.java:264)
        at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.compile(XPathImpl.java:394)
    Last edited by Eleeist; 02-09-2012 at 06:15 PM.

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

    Default Re: Specific attributes in XPath

    You didn't post your Java code.
    Get in the habit of using standard Java naming conventions!

  3. #3
    Eleeist is offline Member
    Join Date
    May 2011
    Posts
    84
    Rep Power
    0

    Default Re: Specific attributes in XPath

    Sorry...

    Java Code:
    String XPathExpression = "/row/@name='test'";
    String XMLUrl = "urltofile";
    
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder  = factory.newDocumentBuilder();
            Document document = builder.parse(new URL(XMLUrl).openStream());
            XPath xpath = XPathFactory.newInstance().newXPath();
            XPathExpression expr = xpath.compile(XPathExpression);
            Object result = expr.evaluate(document, XPathConstants.NODESET);
            NodeList nodes = (NodeList) result;
    
    for (int i = 0; i < nodes.getLength(); i++) {
    
                resultArray[i] = nodes.item(i).getNodeValue();
    
            }
    for (int i = 0; i < resultArray.length; i++) {
                System.out.println(resultArray[i]);
            }
    Last edited by Eleeist; 02-09-2012 at 06:15 PM.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Specific attributes in XPath

    Java Code:
    String XPathExpression = "/row/@name='Test'";
    Your test is wrong.
    That should (IIRC) be in square brackets.
    Java Code:
    String XPathExpression = "/row[@name='Test']";

  5. #5
    Eleeist is offline Member
    Join Date
    May 2011
    Posts
    84
    Rep Power
    0

    Default Re: Specific attributes in XPath

    I changed it. Now the array returns null.

    Basically, what I want to do is to select an row that has "name" attribute "test" (there are many rows with different names) and get all its attributes values (in the example it is only id, but actually there can be more attributes in each row).

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Specific attributes in XPath

    What does your test XML look like?
    The other option (if 'row' is not a top level element) is use a double slash, so it searches right down the tree.
    Fubarable likes this.

  7. #7
    Eleeist is offline Member
    Join Date
    May 2011
    Posts
    84
    Rep Power
    0

    Default Re: Specific attributes in XPath

    This is the actual XML:

    Java Code:
    <eveapi version="2">
    <currentTime>2012-02-09 16:40:28</currentTime>
    <result>
    <rowset name="characters" key="characterID" columns="name,characterID,corporationName,corporationID">
    <row name="characterName1" characterID="1111111111" corporationName="corpName1" corporationID="11111111"/>
    <row name="characterName2" characterID="2222222222" corporationName="corpName2" corporationID="22222222"/>
    </rowset>
    </result>
    <cachedUntil>2012-02-09 17:11:12</cachedUntil>
    </eveapi>
    I want to select a row where name is eg. "characterName1" and get this row's characterID.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Specific attributes in XPath

    Which would have been helpful, as 'row' is clearly not a top element.
    You'll need "//row[@name='test']".

  9. #9
    Eleeist is offline Member
    Join Date
    May 2011
    Posts
    84
    Rep Power
    0

    Default Re: Specific attributes in XPath

    Ok, I still get "null" from the returned array.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Specific attributes in XPath

    You'll have to play around with it.
    Could be namespaces, typos, anything.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Specific attributes in XPath

    I presume you're not actually using 'test' against that data above?

  12. #12
    Eleeist is offline Member
    Join Date
    May 2011
    Posts
    84
    Rep Power
    0

    Default Re: Specific attributes in XPath

    No, I'm not.

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Specific attributes in XPath

    OK:
    Java Code:
             String xml = "<eveapi version=\"2\"> <currentTime>2012-02-09 16:40:28</currentTime> <result> <rowset name=\"characters\" key=\"characterID\" columns=\"name,characterID,corporationName,corporationID\"> <row name=\"characterName1\" characterID=\"1111111111\" corporationName=\"corpName1\" corporationID=\"11111111\"/> <row name=\"characterName2\" characterID=\"2222222222\" corporationName=\"corpName2\" corporationID=\"22222222\"/> </rowset> </result> <cachedUntil>2012-02-09 17:11:12</cachedUntil> </eveapi>";
             String XPathExpression = "//row[@name='characterName1']";
             String XMLUrl = "urltofile";
             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
             DocumentBuilder builder = factory.newDocumentBuilder();
             Document document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
             XPath xpath = XPathFactory.newInstance().newXPath();
             XPathExpression expr = xpath.compile(XPathExpression);
             Object result = expr.evaluate(document, XPathConstants.NODESET);
             NodeList nodes = (NodeList) result;
             System.out.println(nodes.getLength());
    That prints out 1.
    The String contains the xml you posted above, and the expression is what I suggested (modified to take into account the data).
    If it doesn't work on yours then there is something else going on I can't see.

  14. #14
    Eleeist is offline Member
    Join Date
    May 2011
    Posts
    84
    Rep Power
    0

    Default Re: Specific attributes in XPath

    Here is the code that I use:

    Java Code:
    String XPathExpression = "/row/@name='test'";
    String XMLUrl = "urltofile";
    
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder  = factory.newDocumentBuilder();
            Document document = builder.parse(new URL(XMLUrl).openStream());
            XPath xpath = XPathFactory.newInstance().newXPath();
            XPathExpression expr = xpath.compile(XPathExpression);
            Object result = expr.evaluate(document, XPathConstants.NODESET);
            NodeList nodes = (NodeList) result;
    
    for (int i = 0; i < nodes.getLength(); i++) {
    
                resultArray[i] = nodes.item(i).getNodeValue();
    
            }
    for (int i = 0; i < resultArray.length; i++) {
                System.out.println(resultArray[i]);
            }
    The code works with XPaths such as "//row/@*" (lists values of all attributes in all rows) but with "//row[@name='characterName1'] it gives me one null value. I would like it to list all values in that row.

    UP: well, since it contains one null value, it prints out 1...
    Last edited by Eleeist; 02-09-2012 at 07:39 PM.

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Specific attributes in XPath

    Java Code:
             for (int i = 0; i < nodes.getLength(); i++) {
                Element n = (Element) nodes.item(i);
                System.out.println(n.getAttribute("name"));
             }
    That prints out the value of "name", which is characterName1.
    The default implementation getNodeValue() displays null (for some reason I can't fathom), so you can't rely on it.
    So cast to an Element (since that's what you;re working with) and use the methods on there.

  16. #16
    Eleeist is offline Member
    Join Date
    May 2011
    Posts
    84
    Rep Power
    0

    Default Re: Specific attributes in XPath

    I get this error:

    Java Code:
    Exception in thread "main" java.lang.ClassCastException: com.sun.org.apache.xerces.internal.dom.DeferredElementImpl cannot be cast to javax.lang.model.element.Element

  17. #17
    Eleeist is offline Member
    Join Date
    May 2011
    Posts
    84
    Rep Power
    0

    Default Re: Specific attributes in XPath

    I still have no idea why I get this error. Could someone please help?

  18. #18
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Specific attributes in XPath

    It's pretty explicit:
    java.lang.ClassCastException: com.sun.org.apache.xerces.internal.dom.DeferredEle mentImpl cannot be cast to javax.lang.model.element.Element
    You probably imported the wrong Element class.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  19. #19
    Eleeist is offline Member
    Join Date
    May 2011
    Posts
    84
    Rep Power
    0

    Default Re: Specific attributes in XPath

    Thanks. Everything works now when I changed the import.

  20. #20
    Fhernd is offline Member
    Join Date
    Oct 2011
    Location
    Bogotá Colombia
    Posts
    5
    Rep Power
    0

    Question Re: Specific attributes in XPath

    Hi,

    I have read all these post answers (or replies) but I have found anything related of how to extract element attributes. I have, for example, this Yahoo! weather information:

    Java Code:
    <yweather:forecast day="Tue" date="24 Apr 2012" low="9" high="17" text="Cloudy" code="26" />
    (full rss)

    I have tried using this code:

    Java Code:
        public static void extractValuesFromYWeatherUsingXPath( String woeid ) throws ParserConfigurationException, SAXException, XPathExpressionException, IOException
        {
            DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
            domFactory.setNamespaceAware( true );
            DocumentBuilder builder = domFactory.newDocumentBuilder();
            Document doc = builder.parse( "http://weather.yahooapis.com/forecastrss?w=" + woeid + "&u=c" );
            
            XPathFactory factory = XPathFactory.newInstance();
            XPath xpath = factory.newXPath();
            XPathExpression expr = xpath.compile( "//yweather:forecast/@*" );
            
            Object result = expr.evaluate( doc, XPathConstants.NODESET );
            
            NodeList attributes = (NodeList) result;
            
            System.out.println( "Tamahnio: " + attributes.getLength() ); 
           
            for (int i = 0; i < attributes.getLength(); i++ )
            {
                Attr attr = (Attr) attributes.item(i);
                System.out.println( "Name: " + attr.getName() + " - Value: " + attr.getValue() );
            }
        }

    But the results are not I have expected (Tue (from day), 24 Apr 2002 (from date) 9 (from low), 17 (from high), Cloudy (from text), and 26 (from code)).

    Some suggestions about this case? Thanks in advance for any help.

    So long.
    Last edited by Fhernd; 04-24-2012 at 07:33 PM.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 2
    Last Post: 11-08-2008, 11:40 AM
  2. Xpath and Xquery
    By Peter in forum Advanced Java
    Replies: 1
    Last Post: 07-05-2007, 04:00 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
  •