0

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

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

3 Comments

You would need to make the selector $('variable', xml), and you should probably prevent using $(this) twice by writing it to a variable.
Will this actually write both responses to the webpage though. It looks to me (I'm new to Javascript / jquery) that it would write the values to #ProjectName twice, which is not the desired functionality.
@Michael I think now the response is a bit more clear, you need a container on the web page where you can add the values.

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.