0

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

  • Perhaps there are characters in front of the XML declaration? Please show your code and the XML document. – Mathias Müller Jan 14 '15 at 13:20
  • Thanks for your reply, I added the code to the question. –  Jan 14 '15 at 13:22
  • Did it occur to you that you are trying to parse JSON as if it were XML? [This is the page that is fetched](http://maps.googleapis.com/maps/api/directions/json?sensor=false&origin=nottingham&destination=derby) - it's definitely not XML and XPath cannot be used to navigate it. – Mathias Müller Jan 14 '15 at 13:26
  • Replace `json` in `http://maps.googleapis.com/maps/api/directions/json` with `xml` and it should return XML. – geert3 Jan 14 '15 at 13:31
  • Oh dear, I did not notice at all. The URL was provided by my tutor and he told me to use XML parsing so I did not think to check it. Thank you. Replacing JSON with XML removed the error, thanks. –  Jan 14 '15 at 13:31

1 Answers1

0

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.

Community
  • 1
  • 1
Mathias Müller
  • 22,203
  • 13
  • 58
  • 75