Parse custom XML schema with jQuery

I get custom schema data back from an AJAX call, and I need to parse it using jQuery. Any idea how to do this?

Here's the 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>

And here is the AJAX call. I can parse plain XML with the following, but not with the XSD file.

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

       dataType: "html",

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

Any ideas?

+3
source share
2 answers

I have not tested this, but you tried:

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

   dataType: "html",

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

The contextjQuery argument expects a DOM element.

returnhtmlwill be an HTML string according to jQuery ajax() documentationif you set dataTypeas HTML. If this is an XML string, you will need to convert jQuery to an element that you can work with first before using it as a context.

0

$.parseXML .

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

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

0

Source: https://habr.com/ru/post/1712509/


All Articles