Mike James
Mike James

Reputation: 471

Parsing XML with jQuery

I have been having issues with parsing some XML. I have no control over the structure of the XML I receive.

<response>
  <variable name = "variable_name">variable value</variable>
  <variable name = "variable_name">variable value</variable>
</response>

I need to get the variable_name and value and write them to the webpage. However the number of of variables is dynamic.

I have already created functions for parsing different xml responses but the structure I receive is different for these.

How can I adapt the following code for the response?

function parseSystem(xml){
     ControllerFound = true;
      $(xml).find("response").each(function()
       {
            $("#ProjectName").append($(this).find("projectName").text());
});

Cheers

Mike

Upvotes: 0

Views: 148

Answers (1)

pconcepcion
pconcepcion

Reputation: 5641

You can try with:

function parseSystem(xml){
      $('variable', xml).each(function()
       {
            var var_instance = $(this);
            $("#container").append("Name: "+var_instance.attr('name')+"Value: "+ var_instance.text());
       }
}

and having on the html a container to append the values:

 <div id="container"></div>

Upvotes: 1

Related Questions