0

Given the following xml, how do I access the record attributes in node, which is the 2nd record.

<?xml version="1.0" encoding="UTF-8" ?> 
- <dfs:myFields xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:q="http://schemas.microsoft.com/office/infopath/2003/ado/queryFields" xmlns:d="http://schemas.microsoft.com/office/infopath/2003/ado/dataFields" xmlns:dfs="http://schemas.microsoft.com/office/infopath/2003/dataFormSolution" xmlns:xdado="http://schemas.microsoft.com/office/infopath/2003/adomapping" xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2010-03-28T23:16:33" xmlns:xd="http://schemas.microsoft.com/office/infopath/2003" xml:lang="en-au" xdado:dataModified="" xmlns="">
- <dfs:queryFields>
  <q:Table1 ID="" Builders_Name="" Builders_Email_Address="" Date_Instructed="" Instructed_By="" Policy_Number="" Claim_Number="" Sum_Insured_Building="" Customer_Name="" Street_Address="" Town="" Postcode="" Customer_Phone="" Agent_Phone="" Tennant_Phone="" Event="" Excess="" Date_of_Event="" Number_of_Levels="" Wall_Construction="" Roof_Construction="" Asbestos="" Make_Safe_Required="" Dividing_Fence="" Location_of_Damage="" Urgent="" Comments="" Roof="" Leak_Detect="" Inception_date_of_policy="" Renewal_date_of_policy="" Estimated_cost_of_claim="" Case_manager="" Sum_Insured_Contents="" Sum_Insured_Contents1="" Internal_Assessor_required="" Internal_assessor="" Contact_if_not_insured="" Postal_address_if_differenet_to_risk="" /> 
  </dfs:queryFields>

<dfs:dataFields>
  <d:Table1 ID="" Builders_Name="ss" Builders_Email_Address="sdf" Date_Instructed="2011-08-08" Instructed_By="sdv" Policy_Number="ddd" Claim_Number="ddd" Sum_Insured_Building="34" Customer_Name="asdf" Street_Address="asdf" Town=sdf" Postcode="34" Customer_Phone="a" Agent_Phone="" Tennant_Phone="" Event="Storm" Excess="100.00" Date_of_Event="sdf" Number_of_Levels="1" Wall_Construction="sdf" Roof_Construction="Cement Tile" Asbestos="sdf" Make_Safe_Required="sdf" Dividing_Fence="No" Location_of_Damage="asdf" Urgent="No" Comments="asdf" Roof="Yes" Leak_Detect="No" Inception_date_of_policy="2asdf" Renewal_date_of_policy="2asdf" Estimated_cost_of_claim="" Case_manager="asdf" Sum_Insured_Contents="" Sum_Insured_Contents1="" Internal_Assessor_required="No" Internal_assessor="" Contact_if_not_insured="" Postal_address_if_differenet_to_risk="" /> 
  </dfs:dataFields>

Thanks, guys.

2 Answers 2

1

Best to use jquery ajax for this, in my opinion...

In Jquery Ajax it'd be something like the following:

$(document).ready(function(){
$.ajax({
    type: "GET",
    url: "../xml/yourxmlfile.xml", 
    dataType: "xml",
    success: function(xml) 
    {
        $(xml).find('nodename').each(function()
        {
            var attrvalue = $(this).attr('attributename');
            ...
            do what you want here
        }
    }
});
});

If you want to stick to plain javascript, you'd probably need to use regex.

Hope this helps

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

Comments

0

If you don't have jQuery and/or don't retrieve this with a controlled AJAX call then you can use:

var el = document.createElement("xml");
el.innerHTML = docData;  //xml content
var record = el.getElementsByTagName("d:Table1")[0];
var recor_builders_name = record.attributes["builders_name"].value;
document.write(recor_builders_name); //Do what you need with your record

Now, if you use an ajax call, but without jQuery, you can use the responseXML attribute from the call. That element is already a document object, so you can skip the first two lines and set el = yourAjax.responseXML instead (or use directly that attribute if you preferr)

Btw, I assume that xml is a fragment of a much bigger document and that the missing root node close-tag is just a copy-paste error.

Comments

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.