Results 1 to 20 of 24
Thread: Specific attributes in XPath
- 02-08-2012, 08:26 PM #1
Member
- Join Date
- May 2011
- Posts
- 84
- Rep Power
- 0
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?.gif)
Example XML:
My code so far:Java Code:<row id="1" name="test" />
The error:Java Code:/row/@name='test'
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 05:15 PM.
- 02-09-2012, 02:04 AM #2
Re: Specific attributes in XPath
You didn't post your Java code.
Get in the habit of using standard Java naming conventions!
- 02-09-2012, 11:59 AM #3
Member
- Join Date
- May 2011
- Posts
- 84
- Rep Power
- 0
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 05:15 PM.
- 02-09-2012, 12:10 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,468
- Rep Power
- 16
Re: Specific attributes in XPath
Your test is wrong.Java Code:String XPathExpression = "/row/@name='Test'";
That should (IIRC) be in square brackets.
Java Code:String XPathExpression = "/row[@name='Test']";
- 02-09-2012, 05:17 PM #5
Member
- Join Date
- May 2011
- Posts
- 84
- Rep Power
- 0
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).
- 02-09-2012, 05:25 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,468
- Rep Power
- 16
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.
- 02-09-2012, 05:46 PM #7
Member
- Join Date
- May 2011
- Posts
- 84
- Rep Power
- 0
Re: Specific attributes in XPath
This is the actual XML:
I want to select a row where name is eg. "characterName1" and get this row's characterID.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>
- 02-09-2012, 05:54 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,468
- Rep Power
- 16
Re: Specific attributes in XPath
Which would have been helpful, as 'row' is clearly not a top element.
You'll need "//row[@name='test']".
- 02-09-2012, 05:59 PM #9
Member
- Join Date
- May 2011
- Posts
- 84
- Rep Power
- 0
Re: Specific attributes in XPath
Ok, I still get "null" from the returned array.
- 02-09-2012, 06:16 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,468
- Rep Power
- 16
Re: Specific attributes in XPath
You'll have to play around with it.
Could be namespaces, typos, anything.
- 02-09-2012, 06:17 PM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,468
- Rep Power
- 16
Re: Specific attributes in XPath
I presume you're not actually using 'test' against that data above?
- 02-09-2012, 06:27 PM #12
Member
- Join Date
- May 2011
- Posts
- 84
- Rep Power
- 0
Re: Specific attributes in XPath
No, I'm not.
- 02-09-2012, 06:30 PM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 10,468
- Rep Power
- 16
Re: Specific attributes in XPath
OK:
That prints out 1.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());
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.
- 02-09-2012, 06:36 PM #14
Member
- Join Date
- May 2011
- Posts
- 84
- Rep Power
- 0
Re: Specific attributes in XPath
Here is the code that I use:
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.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]); }
UP: well, since it contains one null value, it prints out 1...Last edited by Eleeist; 02-09-2012 at 06:39 PM.
- 02-10-2012, 10:15 AM #15
Moderator
- Join Date
- Apr 2009
- Posts
- 10,468
- Rep Power
- 16
Re: Specific attributes in XPath
That prints out the value of "name", which is characterName1.Java Code:for (int i = 0; i < nodes.getLength(); i++) { Element n = (Element) nodes.item(i); System.out.println(n.getAttribute("name")); }
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.
- 02-10-2012, 05:56 PM #16
Member
- Join Date
- May 2011
- Posts
- 84
- Rep Power
- 0
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
- 02-11-2012, 11:01 AM #17
Member
- Join Date
- May 2011
- Posts
- 84
- Rep Power
- 0
Re: Specific attributes in XPath
I still have no idea why I get this error. Could someone please help?
- 02-11-2012, 11:41 AM #18
Re: Specific attributes in XPath
It's pretty explicit:
You probably imported the wrong Element class.java.lang.ClassCastException: com.sun.org.apache.xerces.internal.dom.DeferredEle mentImpl cannot be cast to javax.lang.model.element.Element
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 02-11-2012, 02:58 PM #19
Member
- Join Date
- May 2011
- Posts
- 84
- Rep Power
- 0
Re: Specific attributes in XPath
Thanks. Everything works now when I changed the import.
- 04-24-2012, 02:28 PM #20
Member
- Join Date
- Oct 2011
- Location
- Bogotá Colombia
- Posts
- 5
- Rep Power
- 0
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:
(full rss)Java Code:<yweather:forecast day="Tue" date="24 Apr 2012" low="9" high="17" text="Cloudy" code="26" />
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 06:33 PM.
Similar Threads
-
how do i print a specific txt file on a specific printer
By nikhilbhat in forum New To JavaReplies: 2Last Post: 11-08-2008, 10:40 AM -
Xpath and Xquery
By Peter in forum Advanced JavaReplies: 1Last Post: 07-05-2007, 03:00 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks