0

This may seem trivial, but I can't figure it out how to 1) retrieve the XML data and 2) find specific content within a child element with this specific XML structure!

XML:

<UploadResults>
  <Header>
    <Header_ID>1</Header_ID> 
    <PlanYear>2015</PlanYear> 
    <PlanVersion>FP01</PlanVersion> 
  </Header>
  <ErrorSet>
    <Row>
      <Row_Id>0</Row_Id> 
      <Error_String>Validation successful with Zero Errors</Error_String> 
    </Row>
    <Row>
       ...
    </Row>
  </ErrorSet>
</UploadResults>

My code:

$.ajax({
    type: "GET",
    url: "testurl",
    dataType: "xml",
    success: function(xml) {
        $(xml).find('UploadResults').each(function(){  
            var errMsg = $(this).find('Error_String').text(); // set variable as content in child 'Error_String'
            $(".validationView").append($(this).text());    // display all xml data on div class
            if (errMsg=="Validation successful with Zero Errors") {
                $("span.proceedBTN").show();
            } 
        });
    }
});

The script works with the following XML Structure so I am not sure why the script doesn't return any data from the first xml structure. (Of course, I would change the root name from UploadResults to Rowsets)

<Rowsets>
  <Rowset>
    <Columns>
      <Column Description="Header_Id"/>
      <Column Description="PlanYear"/>
      <Column Description="PlanVersion"/> 
    </Columns>
    <Row>
      <Row_Id>0</Row_Id> 
      <Error_String>Validation successful with Zero Errors</Error_String> 
    </Row>
    <Row>
       ...
    </Row>
  </Rowset>
</Rowsets>
2
  • Looks correct... what exactly isnt working? Commented Feb 12, 2012 at 0:42
  • it did not return any results from the xml page. I am adding another XML structure that works well with this script... I am not sure if its because of another level in the XML structure.. Commented Feb 12, 2012 at 1:30

1 Answer 1

2

You dont need to find upload results, its the root of your document.

$(xml).each(function(){  
    var errMsg = $(this).find('Error_String').text(); // set variable as content in child 'Error_String'
    $(".validationView").append($(this).text());    // display all xml data on div class
    if (errMsg=="Validation successful with Zero Errors") {
            $("span.proceedBTN").show();
    } 
 });

I just ran it and it worked here is a JSFiddle that shows it in action.

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

1 Comment

I removed the root, but its still not returning any data from my XML. I updated my post to include an XML structure that this script works well with... however, I need it to work with the first xml file.

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.