Reputation: 559
I'm writing code in java and trying to parse the xml from this URL: https://maps.googleapis.com/maps/api/directions/xml?origin=30.606595,33.546753&destination=30.657406,33.712234&sensor=false
This URL is belong to google API, which takes 2 points (src, dest) and return the route between them in xml.
When i debug the program with eclipse, it runs perfect. but when i run the code without debug, it returns an error. (when I put a breakpoint at the end of the function, the "dist" is null and i don't know why) Any idea why it happens?
the code is
public double calcDist(Point p) //p=the src of the ride (the dest in the calculation)
{
String src = Double.toString(this.lati);
src = src.concat(",");
src = src.concat(Double.toString(this.longi));
String dest = Double.toString(p.lati);
dest = dest.concat(",");
dest = dest.concat(Double.toString(p.longi));
String dist=null;
URL url = null;
try
{
url = new URL("https://maps.googleapis.com/maps/api/directions/xml?origin="+src+"&destination="+dest+"&sensor=false");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;
db = dbf.newDocumentBuilder();
Document doc = null;
doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("leg");
for (int i = 0; i < nodeList.getLength(); i++)
{
Node node = nodeList.item(i);
NodeList nodeList2 =node.getChildNodes();
for (int j = 0; j < nodeList2.getLength(); j++)
{
Node child = nodeList2.item(j);
if (child.getNodeName().contentEquals("distance"))
{
NodeList nodeList3 = child.getChildNodes();
for (int p1 = 0; p1 < nodeList3.getLength(); p1++)
{
Node child2 = nodeList3.item(p1);
if (child2.getNodeName().contentEquals("text"))
{
Node tmp = child2.getFirstChild();
if (tmp != null)
dist = child2.getFirstChild().getNodeValue();
}
}
}
}
}
}
catch (ParserConfigurationException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (SAXException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
dist = dist.substring(0, dist.length()-3);
return Double.parseDouble(dist);
}
Upvotes: 0
Views: 3210
Reputation: 2473
I don't run the code. But it seems to me, after checking the XML file, that the distance comes in the format 17,0 km
out of the xml. After your substring it still remains 17,0
, but in Java and other langugages floating numbers using a dot. So this could be your error.
But it could be possible, that something with your parser is wrong.
You could use SAX-Parser API of Java for XML parsing, instead of the slow and unperformant DOM-parser.
BR
Upvotes: -1
Reputation: 1
instead of XML request json object (https://maps.googleapis.com/maps/api/directions/json?origin=30.606595,33.546753&destination=30.657406,33.712234&sensor=false)and use a java library such as: http://sites.google.com/site/gson/gson-user-guide to convert it to a java object and pull whatever you need. XML is heavy in your case.
Upvotes: 0