Results 1 to 7 of 7
  1. #1
    thooom is offline Member
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

    Default 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:
    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;
    }
    Basically when I run this in my client main class with the following code, I just get test as a printout:
    Java Code:
    XMLReader player = new XMLReader("Thooom");
    player.finnPSR();
    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']");

    Does it match with the code underneath?
    Java Code:
    <player_stats aid="1234">
     <stat name="acc_pub_skill">1234</stat>

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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!

  3. #3
    thooom is offline Member
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

    Default

    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:
    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>
    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:
    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);
            }
        }
    }
    Apparently xp=com.sun.org.apache.xpath.internal.jaxp.XPathExp ressionImpl@601bb1

    :(

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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']";
    ?

  5. #5
    thooom is offline Member
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

    Default

    Ok, tried adding the part you said in various shapes and sizes. But still some error somewhere.

    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);
    
            }
        }
    }
    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:
    Error= java.lang.NullPointerException

    Any thoughts on where to look for the error(s)?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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();
        }
      }
    }

  7. #7
    thooom is offline Member
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

    Default

    The NPE was thrown at the catch (Exception err) it seems. But nevermind that, you made it work. Rejoice! Spent 13 hours trying to figure this out yesterday.

    Thank you veryvery much :)

Similar Threads

  1. Problems with XPath (XML Parsing)
    By thooom in forum New To Java
    Replies: 6
    Last Post: 04-25-2010, 04:56 PM
  2. problems with parsing a string into int,
    By bigj in forum New To Java
    Replies: 3
    Last Post: 01-06-2010, 10:32 PM
  3. Replies: 0
    Last Post: 11-13-2009, 09:51 PM
  4. Replies: 3
    Last Post: 09-11-2008, 03: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
  •