2009-07-13 19 views
3

我從AJAX調用中獲取自定義架構數據,我需要使用jQuery解析它。任何想法如何做到這一點?使用jQuery解析自定義XML架構

這裏的XML:

<xsd:get_customer_summary_response xmlns:xsd="http://com/acmeco/ovm/cas/xsd"> 
    <xsd:customer_details> 
    <typ:phone_number xmlns:typ="http://com/acmeco/ovm/cas/types">1.555.5553002</typ:phone_number> 
    <typ:timezone xsi:nil="true" xmlns:typ="http://com/acmeco/ovm/cas/types" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/> 
    <typ:zipcode xmlns:typ="http://com/acmeco/ovm/cas/types">3002</typ:zipcode> 
... 
    </xsd:customer_details> 
</xsd:get_customer_summary_response> 

而這裏的AJAX調用。我可以用下面的解析正常的XML,但不能解析XSD的東西。

$.ajax({ 
     type: "GET", 
     url: "so.xml", 

     dataType: "html", 

     success: function(returnhtml){ 
    $("customer_details", returnhtml).find("zipcode").each(function() { 
     alert($(this).text()); 
    }); 
    }, etc. 

任何想法?

回答

0

我沒有測試過這一點,但你嘗試過:

$.ajax({ 
    type: "GET", 
    url: "so.xml", 

    dataType: "html", 

    success: function(returnhtml){ 
    $(returnhtml).find("customer_details zipcode").each(function() { 
     alert($(this).text()); 
    }); 
}, etc. 

的jQuery的context參數需要一個DOM元素。

returnhtml如果將dataType設置爲HTML,則它將成爲根據jQuery's ajax() documentation的HTML字符串。如果它是一個XML字符串,那麼在將它用作上下文之前,您需要先將jQuery轉換爲您可以使用的元素。

0

您可以使用$.parseXML

success: function (returnhtml) { 
    var parsedXML = $.parseXML(returnhtml); 
    $(parsedXML).find("zipcode").each(function() { 
     alert($(this).text()); 
    }); 
} 

https://jsfiddle.net/chukanov/jjt894dc/