2

https://jsfiddle.net/recklesswish/6yw1tdwh/5/ The following link has the JSON and i have converted this particular JSON in XML. Now the issue is I am not getting that how to parse it using javascript. Here is the code of xml, JSON resp. XML

var carsData = "<cars>
  "<Honda>"
    "<model>Figo</model>"
  "</Honda>"
  "<Honda>"
    "<model>City</model>"
  "</Honda>"
  "<Audi>"
    "<model>A6</model>"
  "</Audi>"
  "<Audi>"
    "<model>A8</model>"
  "</Audi>"
"</cars>"

JSON

var carsData = '{"cars":{"Honda":[{"model":"Figo"},{"model":"City"}],"Audi":[{"model":"A6"},{"model":"A8"}]}}';

$('#newCollection').on('click', function() {
  $(this).empty(); 
  var data = JSON.parse(carsData);
  $(this).append('New Collection<ul></ul>');

  for (var make in data.cars) {
    for (var i = 0; i < data.cars[make].length; i++) {
      var model = data.cars[make][i].model;
      $(this).find('ul').append('<li>' + make + ' - ' + model + '</li>')
    }
  }
});

With HTML

<ul>
  <li id="newCollection">New Collection</li>
</ul>
  • Possible duplicate of [Cross-Browser Javascript XML Parsing](http://stackoverflow.com/questions/7949752/cross-browser-javascript-xml-parsing) – Thomas Francois Apr 21 '16 at 09:18
  • It could be. But it is not working for my nested tree. –  Apr 21 '16 at 09:20
  • 1
    Possible duplicate of [How to parse XML using jQuery?](http://stackoverflow.com/questions/7228141/how-to-parse-xml-using-jquery) – kemicofa ghost Apr 21 '16 at 09:30
  • What are you trying to achieve exactly? Your question may be a case of a [XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)... – Thomas Francois Apr 21 '16 at 09:31

2 Answers2

1

First of all make your xml look like this in javascript -

var carsData = "<cars><Honda><model>Figo</model></Honda><Honda><model>City</model> </Honda><Audi><model>A6</model></Audi><Audi><model>A8</model></Audi></cars>"

And your JS code would look like this -

$('#newCollection').on('click', function() {
                        $(carsData).find("Honda").each(function() {
                            var model = $(this).find("model").text();
                            console.log("model: " + model );
                        });
                    });

newCollection is the id of your button. This will print model as below-

model: Figo
model: City

I would suggest you to go through basics first rather than trying to jump into coding directly. Clear basics will make you a good coder in long run :)

Amar
  • 321
  • 3
  • 14
  • var carsData = "HondaFigoHondaCityAudiA6AudiA8"; $('#newCollection').on('click', function() { var $li = $(this); $li.empty(); var data = $($.parseXML(carsData)); $(this).append('New Collection
      '); data.find('cars').children().each(function() { var $make = $(this); is it right ?? ANd how to loop the xml node and access the values from the node.? Thanks for your valuable reply.
      –  Apr 21 '16 at 10:48
    0

    I see. Use below code to achieve what you want -

    <body> <h1>Hello</h1> <button type="button" id="newCollection">Click Me!</button> <ul id="myul"></ul> </body>

    And your JS code should be -

    $('#newCollection').on('click', function() {
    
        var data = $($.parseXML(carsData));                         
        data.find('cars').children().each(function() { 
        var make = $(this).find("model").text();
        $('#myul').append('<li>'+make+'</li>'); 
        });
    });
    
    Amar
    • 321
    • 3
    • 14