如何在jQuery中解析这个XML,只存储属性值?

问题描述:

我已经通过$。员额(叫这个XML),这是返回值:如何在jQuery中解析这个XML,只存储属性值?

<processResponse xmlns:client="http://xmlns...." xmlns="http://xmlns..." xmlns:wsa="http://www.w3.org/2005/08/addressing" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> 
    <ns2:ProcessResult xmlns:ns2="http://xmlns..."> 
     <ns2:StatusCode>SUCCESS</ns2:StatusCode> 
     <ns2:StatusMessages> 
     <ns2:StatusMessage> 
      <ns2:Code>0</ns2:Code> 
      <ns2:Message>Success</ns2:Message> 
     </ns2:StatusMessage> 
     </ns2:StatusMessages> 
    </ns2:ProcessResult> 
    <premises> 
     <ns3:cwsPremise xmlns:ns3="http://xmlns.."> 
     <ns3:cwsPremiseService> 
      <ns3:cwsPremiseHeader PremiseID="5855654654"/> 
      <ns3:cwsPremiseDetails PremiseID="5855654654" PremiseType="BBBB" Address="3892 A street" City="PA" Postal="96456" PremiseDataArea="PB" PremiseInfo="b55" District="a333" IsMultiple="N" AllowSelfService="Y" NeedAppointment="N"/> 
     </ns3:cwsPremiseService> 
     </ns3:cwsPremise> 
     <ns3:cwsPremise xmlns:ns3="http://xmlns..."> 
     <ns3:cwsPremiseService> 
      <ns3:cwsPremiseHeader PremiseID="99565423587"/> 
      <ns3:cwsPremiseDetails PremiseID="99565423587" PremiseType="AAAA" Address="123 Main street" City="SC" Postal="98652" PremiseDataArea="AE" PremiseInfo="K876" District="b999" IsMultiple="N" AllowSelfService="Y" NeedAppointment="N"/> 
     </ns3:cwsPremiseService> 
     </ns3:cwsPremise> 
    </premises> 
</processResponse> 

如何解析的属性,如PremiseID,PremiseType,地址,邮政,PremiseDataArea,PremiseInfo,区即在<ns3:cwsPremiseDetails PremiseID="5855654654" PremiseType="BBBB" Address="3892 A street" City="PA" Postal="96456" PremiseDataArea="PB" PremiseInfo="b55" District="a333" IsMultiple="N" AllowSelfService="Y" NeedAppointment="N"/>

这是我的尝试:

$.post(url, function(xml) { 

    var $PremiseID = $(xml).find('cwsPremiseDetails').attr('PremiseID').text(); 

    console.log($PremiseID); 
    // output: nothing 

}); 
+1

检查错误控制台,也'ATTR( )'返回一个字符串。 – Musa

+0

不错,但它返回undefined。 – nolabel

的JQuery :: ATTR()返回一个字符串,而不是一个jQuery对象。尝试没有.text()调用。

var $PremiseID = $(xml).find('cwsPremiseDetails').attr('PremiseID'); 
+0

输出未定义。 – nolabel

首先尝试解析XML,如果不指定具体的数据类型为XML它不会返回一个XMLDocument,但只是一个字符串

$.post(url, function(xml) {  
    var $PremiseID = $($.parseXML(xml)).find('cwsPremiseDetails').attr('PremiseID');  
    console.log($PremiseID);  
}); 
+0

控制台日志=未定义 – nolabel