• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Retrieve values for a complicate XML file (Java)

 
Pan Niko
Ranch Hand
Posts: 86
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I didn't use XML files for a very long time and I have forgot how to interact with complicate files. Need to retrieve the value of an Element in an XML file that is sitting too deep in that. For example:

I want to retrieve the latitude value that appears here: DirectionsResponse/route/leg/step/start_location/lat. Need to do the same for Longitude and latitude/Longitude for the end_location. "leg" contains more elements but I want just "step". Also, "step" elements are not always the same quantity, so need to check the size of the "leg" or how many "step" contains. I made some tries but all have failed! Please anybody knows how my for loop should be in order to getting correctly all "start_location" and "end_location" from all "step"?


Thanks in advance,
toto_7
 
David Nicholls
Greenhorn
Posts: 6
IBM DB2 Eclipse IDE VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use XPath.

http://www.w3schools.com/xpath/

XPath is very easy to learn and part of the JRE since Version 5.

 
Pan Niko
Ranch Hand
Posts: 86
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's cool thanks for quick reply. There is a problem however. Cause I'm working on Android API 7 and need to stick on that API, I can't use XPath stuff. There is any way to make it work on API 7 or there is an alternative way using SAX Parser?

Thanks in advance,
toto_7
 
Paul Clapham
Sheriff
Posts: 21152
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're limited to using only SAX, then you won't be writing any loops anyway. Let's see what code you have so far and maybe you could ask a question about that code.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Jaxen library implements XPath and works fine on Android 1.x. It's what I use to process geo XML on Android.
 
Pan Niko
Ranch Hand
Posts: 86
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I believe is better to stick to current way, because I don't want to mess with new libs. So for example I have a file like this .. link. As you can see contains a lot of <step>. So I want to retrieve from each <step> the <start_location>/<lat>,<lng> and <end_location>/<lat>,<lng>. How I will be able to do that on API 7? For each route the <step> element vary...

Thanks in advance,
toto_7
 
Paul Clapham
Sheriff
Posts: 21152
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me move this to the Android forum, then, if it's a question about Android programming.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's wrong with adding the library? It's not like it's big.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic