1

I have an xml like below I am trying to parse using jquery and get something like i wrote below java script to parse it.I have tried below code but i am able to get location not in below format as i expect.

Expected Result:

S1:E1:https://location1
S2:E2:https://location1

xml:

<Country>
    <State>
        <id>S1</id>    
        <City>
            <E1>    
               <location>https://location1</location>
            </E1>    
        </City>
        <County>
            <E2>    
               <location>https://location2</location>            
            </E2>
        </County>
    </State>    
    <State>
        <id>S2</id>
        <City>
            <E1>    
               <location>https://location3</location>
            </E1>    
        </City>
        <County>
            <E2>    
               <location>https://location4</location>            
            </E2>    
        </County>    
    </State>
</Country>  

code

var container = $( document.body );    
var xml = '<Country> <State> <id>S1</id> <City> <E1> <location>https://location1</location> </E1> </City> <County> <E2> <location>https://location2</location> </E2> </County> </State> <State> <id>S2</id> <City> <E1> <location>https://location3</location> </E1> </City> <County> <E2> <location>https://location4</location> </E2> </County> </State> </Country>',
xmlDoc = $.parseXML( xml ),
$xml = $( xmlDoc ),
$location = $xml.find( 'location' );    
container.append( $("<p/>", { "text" : $location.text() }) ); 

1 Answer 1

1

Try it like,

var container = $( document.body );

var xml = '<Country> <State> <id>S1</id> <City> <E1> <location>https://location1</location> </E1> </City> <County> <E2> <location>https://location2</location> </E2> </County> </State> <State> <id>S2</id> <City> <E1> <location>https://location3</location> </E1> </City> <County> <E2> <location>https://location4</location> </E2> </County> </State> </Country>',
xmlDoc = $.parseXML( xml ),
$xml = $( xmlDoc ),
$location = $xml.find( 'location' );   // will return all location elements     
$location.each(function(){
    container.append( $("<p/>", { "text" : $(this).text() }) ); 
});

Demo

Updated code to display result with ID

var container = $(document.body);

var xml = '<Country> <State> <id>S1</id> <City> <E1> <location>https://location1</location> </E1> </City> <County> <E2> <location>https://location2</location> </E2> </County> </State> <State> <id>S2</id> <City> <E1> <location>https://location3</location> </E1> </City> <County> <E2> <location>https://location4</location> </E2> </County> </State> </Country>',
    xmlDoc = $.parseXML(xml),
    $xml = $(xmlDoc),
    $location = $xml.find('location'); // will return all location elements     
$location.each(function () {
    container.append($("<p/>", {
        "text": 
            $(this).closest('State').find('id').text() + ':' + // get State ID like S1,S2
            $(this).closest('City').find(':first-child').prop('tagName') + ':' + // get City's location tag like E1,E2
            $(this).text() // get the location text like location1,location2....
    }));
});

Updated Demo

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks but i want to know from where this location came from eg: S1:E1:location1 S2:E2:location4
@user2656713 see my updated answer, you need to make the XML in a format to get proper result for location2 and location4, currently it shows undefined. You can use a if-condition to by pass when the location tag doesn't exists.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.