Results 1 to 7 of 7
- 04-25-2010, 03:22 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 10
- Rep Power
- 0
Problems with parsing using XPath
I've made this method in my hopefully-soon-to-be XML parser. Making a small program that will parse a handful of stats from the game Heroes of Newerth. However my method keeps returning the value 0.
The xml file I'm trying to parse is at xml.heroesofnewerth.com
This is my code:
Basically when I run this in my client main class with the following code, I just get test as a printout:Java Code:public class XMLReader { String url = "http://xml.heroesofnewerth.com/xml_requester.php?f=player_stats&opt=nick&nick[]="; private String player; String psr; XMLReader (String player) { this.player = player; } public String finnPSR() throws ParserConfigurationException, SAXException, IOException, XPathExpressionException { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(true); // never forget this! DocumentBuilder builder = domFactory.newDocumentBuilder(); org.w3c.dom.Document doc = builder.parse("http://xml.heroesofnewerth.com/xml_requester.php?f=player_stats&opt=nick&nick[]=Bahada"); XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); XPathExpression expr = xpath.compile("*[@name='acc_pub_skill']"); psr = xpath.evaluate("*[@name='acc_pub_skill']", doc); System.out.print(psr + "test"); return psr; }
My guess is that this line is the one with the flaws. Having some problems understanding the syntax for xml's that have attributes and values. xpath.compile("*[@name='acc_pub_skill']");Java Code:XMLReader player = new XMLReader("Thooom"); player.finnPSR();
Does it match with the code underneath?
Java Code:<player_stats aid="1234"> <stat name="acc_pub_skill">1234</stat>
-
Question: does the XML file use a namespace?
I'm no XPath expert but have used it, and usually if I have a problem with it, I do what I do for other Java bugs: create a small program just for the purposes of demonstrating and solving the problem and test it on a small local XML file that is similar to the XML file that you want your code to work on, but again, smaller and simpler.
Then if you still can't get your code to work, you can post your small self-contained program and XML file here and we can work on it.
Best of luck!
- 04-25-2010, 04:40 PM #3
Member
- Join Date
- Mar 2010
- Posts
- 10
- Rep Power
- 0
Hi, thanks for the tip. The code I had there is really small though. But I can try an even simpler one. At the moment I am reading up on Namespace to figure out what it is. My English is limited and everywhere I go Namespace is explained with so many big words. (Looking at Wikipedia and W3Schools).
The xml looks like this (Just the first lines). Anyway from what I could find out xml documents declare if they are using Namespaces at the end, but this one doesn't seem to do that. Then again I'm not really understanding this yet, so I could be mistaken:
Been trying a different approach without any particular luck too. Think this is my 4th attempt, but deffo the simplest one. Still no luck though.Java Code:<?xml version="1.0" encoding="UTF-8" ?><stats> <player_stats aid='25'> <stat name='nickname'>S2Moebiwan</stat> <stat name='acc_games_played'>26</stat> <stat name='acc_wins'>12</stat> <stat name='acc_losses'>14</stat> <stat name='acc_concedes'>31</stat>
Apparently xp=com.sun.org.apache.xpath.internal.jaxp.XPathExp ressionImpl@601bb1Java Code:public class XMLParse { public static void main(String[] args) { String acc_games = "*[@name='acc_games']"; String url = "http://xml.heroesofnewerth.com/xml_requester.php?f=player_stats&opt=nick&nick[]=Bahada"; try { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); Document doc = docBuilder.parse(url); XPath xpath = XPathFactory.newInstance().newXPath(); doc.getDocumentElement().normalize(); XPathExpression accg = xpath.compile(acc_games); System.out.println("xp=" + accg); } catch (Exception err) { System.out.println("Error= " + err); } } }
:(
-
To get the NodeList, you must call evaluate on the XPathExpression object. Please look here for details: The Java XPath API
I don't have a Java compiler located where I am, so I can't test your XPath expression, but I'm wondering if you need to prefix it with //*, i.e.,
?Java Code:String acc_games = "//*[@name='acc_games']";
- 04-25-2010, 05:52 PM #5
Member
- Join Date
- Mar 2010
- Posts
- 10
- Rep Power
- 0
Ok, tried adding the part you said in various shapes and sizes. But still some error somewhere.
Bold text is the new parts. Could it be something wrong with the String url line? I've never found an url quite like this one in any example or tutorial on the internet. Always book.xml or something. And this page is basically php. It does print out the following now:Java Code:public class Evaluate { public static void main(String[] args) { [B] String acc_games = "//*[@name='acc_games']";[/B] String url = "http://xml.heroesofnewerth.com/xml_requester.php?f=player_stats&opt=nick&nick[]=Bahada"; try { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); Document doc = docBuilder.parse(url); XPath xpath = XPathFactory.newInstance().newXPath(); doc.getDocumentElement().normalize(); XPathExpression accg = xpath.compile(acc_games); [B] Object result = accg.evaluate(doc, XPathConstants.NODESET); NodeList nodes = (NodeList) result; System.out.println("test" + nodes.item(0).getNodeValue());[/B] } catch (Exception err) { System.out.println("Error= " + err); } } }
Error= java.lang.NullPointerException
Any thoughts on where to look for the error(s)?
-
Where is the NPE being thrown?
One problem I see is that you need to extract the info out of the node when you've got one. For instance:
Java Code:Object result = accg.evaluate(doc, XPathConstants.NODESET); NodeList nodes = (NodeList) result; for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); System.out.print(node.getNodeName()); System.out.println("\t" + node.getTextContent()); }
or to put together:
Java Code:import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Document; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class XMLParse { public static void main(String[] args) { //String acc_games = "*[@name='acc_games']"; String acc_games = "//*[@name='acc_games_played']"; //String docPath = "accPlayers.xml"; try { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); String url = "http://xml.heroesofnewerth.com/xml_requester.php?f=player_stats&opt=nick&nick[]=Bahada"; //Document doc = docBuilder.parse(XMLParse.class.getResourceAsStream(docPath)); Document doc = docBuilder.parse(url); XPath xpath = XPathFactory.newInstance().newXPath(); //doc.getDocumentElement().normalize(); XPathExpression accg = xpath.compile(acc_games); Object result = accg.evaluate(doc, XPathConstants.NODESET); NodeList nodes = (NodeList) result; for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); System.out.print(node.getNodeName()); System.out.println("\t" + node.getTextContent()); } } catch (Exception err) { err.printStackTrace(); } } }
- 04-26-2010, 09:47 AM #7
Member
- Join Date
- Mar 2010
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
Problems with XPath (XML Parsing)
By thooom in forum New To JavaReplies: 6Last Post: 04-25-2010, 04:56 PM -
problems with parsing a string into int,
By bigj in forum New To JavaReplies: 3Last Post: 01-06-2010, 10:32 PM -
any parser for parsing XPath expression into an data structure?
By yuyu200 in forum XMLReplies: 0Last Post: 11-13-2009, 09:51 PM -
How can we write case insensitive queries for Xpath xml parsing
By vijayvcs in forum XMLReplies: 3Last Post: 09-11-2008, 03:00 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks