aspose file tools*
The moose likes Android and the fly likes Retrieve values for a complicate XML file (Java) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Android
Bookmark "Retrieve values for a complicate XML file (Java)" Watch "Retrieve values for a complicate XML file (Java)" New topic
Author

Retrieve values for a complicate XML file (Java)

Pan Niko
Ranch Hand

Joined: Mar 19, 2011
Posts: 86

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

Joined: Oct 10, 2011
Posts: 6

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

Joined: Mar 19, 2011
Posts: 86

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
Bartender

Joined: Oct 14, 2005
Posts: 18882
    
    8

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
Marshal

Joined: Mar 22, 2005
Posts: 42595
    
  65
The Jaxen library implements XPath and works fine on Android 1.x. It's what I use to process geo XML on Android.


Ping & DNS - my free Android networking tools app
Pan Niko
Ranch Hand

Joined: Mar 19, 2011
Posts: 86

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
Bartender

Joined: Oct 14, 2005
Posts: 18882
    
    8

Let me move this to the Android forum, then, if it's a question about Android programming.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42595
    
  65
What's wrong with adding the library? It's not like it's big.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Retrieve values for a complicate XML file (Java)