0

Say I have this XML, which repeats itself:

<sample>
    <org.postgis.Point>
     <dimension>2</dimension>
     <haveMeasure>false</haveMeasure>
     <type>1</type>
     <srid>4326</srid>
     <x>-73.43975830078125</x>
     <y>42.0513801574707</y>
     <z>0.0</z>
     <m>0.0</m>
    </org.postgis.Point>
<sample>

And am using jQuery to try to get the x and y coordinates out of it. How do I do that?

   $(xml).find('sample').each(function(){
      $(this).find('org.postgis.Point').each(function(){
         var x = $(this).find('x').text();

Is this the right idea? Is there a less verbose way to get inside nested tags?

1 Answer 1

3

First of all, you'll need to escape the .s, because in CSS selectors, they represent classes. Next, you can drop a level of eaches by using the descendant combinator, :

$(xml).find('sample org\\.postgis\\.Point').each(function() {
     var x = $(this).find('x').text();

And of course, if there's only one org.postgis.Point:

var x = $(xml).find('sample org\\.postgis\\.Point x').text();
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.