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>
4
  • Possible duplicate of Cross-Browser Javascript XML Parsing Commented Apr 21, 2016 at 9:18
  • It could be. But it is not working for my nested tree. Commented Apr 21, 2016 at 9:20
  • 1
    Possible duplicate of How to parse XML using jQuery? Commented Apr 21, 2016 at 9:30
  • What are you trying to achieve exactly? Your question may be a case of a XY Problem... Commented Apr 21, 2016 at 9:31

2 Answers 2

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 :)

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

1 Comment

var carsData = "<cars><make>Honda<model>Figo</model></make><make>Honda<model>City</model></make><make>Audi<model>A6</model></make><make>Audi<model>A8</model></make></cars>"; $('#newCollection').on('click', function() { var $li = $(this); $li.empty(); var data = $($.parseXML(carsData)); $(this).append('New Collection<ul></ul>'); 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.
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>'); 
    });
});

Comments

Your Answer

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