1

I'm using jQuery.

$.ajax({
   url: xxx,
   success: function(data) {
      ...
   }
});

The data is an XML document like:

<root>
   <source>
      <a><source>...</source></a>
      <b>...</b>
      ...
   </source>
   <article>
   ...
   </article>
</root>

I want to extract the XML fragment under source tag, and append them to a div with id "converted". How could I do? PS: the fragment may include source tags too.

  • What did you try? what happened? Where did you get stuck? Did you look at other questions (randomly, like this one: http://stackoverflow.com/questions/8498098/parse-xml-with-jquery-ajax-request?rq=1 ) – Nanne Dec 18 '13 at 09:10
  • There is nothing for AJAX here in particular. You should look for parse XML using JavaScript or JQuery. Something like http://stackoverflow.com/q/7228141/1654121 – Vivek Jain Dec 18 '13 at 09:12
  • @Nanne, I don't know howto handle data. I've tried $(data), but it does not work as I expected. I mean I don't know howto do next with $(data). I mentioned ajax, because data is returned from ajax, and it seems like it's the XMLDocument type. – user3114401 Dec 18 '13 at 09:12
  • @theghostofc I'll try parseXML. – user3114401 Dec 18 '13 at 09:13

3 Answers3

1

Try this:

$('#converted').append($('source:first', data));
0
var txt = data
if (window.DOMParser)
{
    parser = new DOMParser();
    xmlDoc = parser.parseFromString(txt, "text/xml");
}
else // Internet Explorer
{
    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async = false;
    xmlDoc.loadXML(txt); 
}

var array_of_source_elems = xmlDoc.getElementsByTagName("source");

xmlDoc can then be used like DOM Documents e.g.: xmlDoc.getElementsBy... etc.

Vivek Jain
  • 3,811
  • 6
  • 30
  • 47
tuxmania
  • 906
  • 2
  • 9
  • 28
0

if you are getting XML documents from ajax, try this

Documentation & Source: https://github.com/josefvanniekerk/jQuery-xml2json

$.get('data/temp.xml', function(xml) {
          var jObj = $.xml2json(xml);
          alert(jObj.node.node1.name[0]["Hello"]);
    });
patel.milanb
  • 5,822
  • 15
  • 56
  • 92