0

I'm trying to make a function that will take url and xpath as arguments, and query the xml file from supplied url and return String results. Here's my code: `package

uforia.tests.daoTests;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;

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.junit.Assert;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

public class XpathHelper {

    public static final String NL = System.getProperty("line.separator");  

    @Test
    public void testBelow() {

        System.out.println(xmlQuery("http://abcnews.go.com/US/wireStory/10-things-today-19933443", "//*[@id=\"storyText\"]/p[3]"));

        Assert.assertTrue(true);
    }


    public String xmlQuery(String url, String xpath) {

        StringBuilder sb = new StringBuilder();

        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); // Getting
                                                                                  // the
                                                                                  // instance
                                                                                  // of
                                                                                  // DocumentBuilderFactory
        domFactory.setNamespaceAware(true); // true if the parser produced will
                                            // provide support for XML
                                            // namespaces;

        try {
            DocumentBuilder builder = domFactory.newDocumentBuilder();
            // Creating document builder

            Document doc = builder.parse(new URL(url).openStream()); // e.g.

            XPath xPath = XPathFactory.newInstance().newXPath();
            // getting instance of xPath

            XPathExpression expr = xPath.compile(xpath);
            // e.g. "//@id"
            NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);

            for (int i = 0; i < nodes.getLength(); i++) {
                sb.append(nodes.item(i).getNodeValue()).append(NL);
            }

        }

        catch (Exception e) {
            e.printStackTrace();
        }
        // Think of closing connection in finnaly branch...
        return sb.toString();

    }

}
`

And I'm getting this error:

[Fatal Error] :37:108: The reference to entity "asset" must end with the ';' delimiter. org.xml.sax.SAXParseException; lineNumber: 37; columnNumber: 108; The reference to entity "asset" must end with the ';' delimiter.

I think the problem is with escaping ampersands, but I can't get it to work.

Thank for help in advance...

Smit
  • 4,685
  • 1
  • 24
  • 28
Nemanja Pavlovic
  • 81
  • 1
  • 1
  • 2
  • [--> This could help you <--](http://stackoverflow.com/questions/6483807/the-reference-to-entity-w-must-end-with-the-delimiter-exception) – Smit Aug 12 '13 at 16:39
  • Why not use a library instead? For example, see http://stackoverflow.com/questions/113063/java-rss-library – Jens Erat Aug 13 '13 at 07:36

0 Answers0