views:

722

answers:

2

I am getting 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"&gt;
  <xsd:customer_details>
    <typ:phone_number xmlns:typ="http://com/acmeco/ovm/cas/types"&gt;1.555.5553002&lt;/typ:phone_number&gt;
    <typ:timezone xsi:nil="true" xmlns:typ="http://com/acmeco/ovm/cas/types" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/&gt;
    <typ:zipcode xmlns:typ="http://com/acmeco/ovm/cas/types"&gt;3002&lt;/typ:zipcode&gt;
...
  </xsd:customer_details>
</xsd:get_customer_summary_response>

And here's the AJAX call. I can parse normal XML with the below, but not XSD stuff.

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

       dataType: "html",

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

Any ideas?

A: 

I haven't tested this, but have 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 context argument of jQuery expects a DOM element.

returnhtml will be an HTML string according to jQuery's ajax() documentation if you set the dataType as HTML. If it's an XML string, you'd need to have jQuery convert it into an element you can work with first, before using it as context.

Dan Herbert
+2  A: 

Here's a post on the Stack that answers this question:

http://stackoverflow.com/questions/853740/jquery-xml-parsing-with-namespaces/858310#858310

CalebHC
Gah, you know I saw that page, but somehow my mind editing out the second "\". I had been trying it with one... This works.
thermans
Don't you hate it when we make mistakes like that? :-) Glad you got it working now.
CalebHC