Search code examples
javaxmlxpathgoogle-directions-api

(Java) Error parsing google directions XML using XPath


I'm trying to get driving directions between two locations using the google directions api.

I have tried to parse the XML returned using different methods but I am always receiving the following error.

javax.xml.xpath.XPathExpressionException: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.

//code redacted


Solution

  • Now that we have established that's indeed the only problem, I'm adding it as an answer.

    The page you request is not XML, but JSON. The SAX parser returns an error because the parser input is not a well-formed XML document. It doesn't have to be, since it's valid JSON.

    As suggested by @geert3, just replace json with xml in the URL:

    http://maps.googleapis.com/maps/api/directions/xml?sensor=false&origin=nottingham&destination=derby
    

    Or else, parse JSON with a JSON parser and use JSONPath instead of XPath to query it.