Jump to content

Retrieve values for a complicate XML file

- - - - -

  • Please log in to reply
1 reply to this topic

#1
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
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

"Programming is like sex. One mistake and you have to support it for the rest of your life."

-Michael Sinz

#2
BlackRabbit

BlackRabbit

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 265 posts
  • Location:ten steps forward
Here you have some good old sample code :

txt="<bookstore><book>";
txt=txt+"<title>The life of the javascript coder</title>";
txt=txt+"<author>Giada De Laurentiis</author>";
txt=txt+"<year>2008</year>";
txt=txt+"</book></bookstore>";

if (window.DOMParser)
  {
  parser=new DOMParser();
  xmlDoc=parser.parseFromString(txt,"text/xml");
  }
else // Internet Explorer
  {
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async=false;
  xmlDoc.loadXML(txt);
  }

it works with the xmlDoc object in windows, understanding you are working in ASP.NET / ajax

you can always parse it with RegExp :
here is a reference :

http://www.regular-e...iptexample.html




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users