Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/414.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javascript访问xml中的属性_Javascript_Xml - Fatal编程技术网

使用javascript访问xml中的属性

使用javascript访问xml中的属性,javascript,xml,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.m

给定以下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>

- 
- 

在我看来,最好使用jQueryAjax来实现这一点

在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,可能需要使用正则表达式


希望这对您有所帮助

如果您没有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标记只是一个复制粘贴错误