使用javascript访问xml中的属性

时间:2011-08-16 03:18:34

标签: javascript xml

给定以下xml,如何访问节点中的记录属性,即第二条记录。

<?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>

谢谢,伙计们。

2 个答案:

答案 0 :(得分:1)

在我看来,最好使用jquery ajax ......

在Jquery Ajax中,它类似于以下内容:

$(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
        }
    }
});
});

如果你想坚持使用普通的javascript,你可能需要使用正则表达式。

希望这有帮助

答案 1 :(得分:0)

如果你没有jQuery和/或没有通过受控的AJAX调用来检索它,那么你可以使用:

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

现在,如果您使用ajax调用但没有jQuery,则可以使用调用中的responseXML属性。该元素已经是文档对象,因此您可以跳过前两行并改为设置el = yourAjax.responseXML(如果您愿意,可直接使用该属性)

顺便说一句,我认为xml是一个更大文档的片段,缺少的根节点close-tag只是一个复制粘贴错误。