Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert xml to string with jQuery

I'm loading an xml file with jQuery ajax loader, and need to convert it to a string so that I can save it out again using PHP post variables. What is the best way to do this?

<script type='text/javascript'>  jQuery.ajax({     type: "GET",     url: "data.xml",     dataType: "xml",     success: parseXML     });   function parseXML(xml) {      var xml_string = jQuery(xml).text();  // (This doesn't work- returns tagless, unformatted text)      alert(xml_string);  }  </script> 
like image 646
Yarin Avatar asked Jun 28 '11 13:06

Yarin


People also ask

How to parse XML in jQuery?

The parseXML() method in jQuery is used to parse a string into an XML document. It uses native methods of the browser for creating a valid XML document. This valid XML document can be passed to jQuery for creating a jQuery object that can be manipulated or traversed.

Does jQuery work with XML?

jQuery can be used for XML processing on the Web as well as HTML processing, and in this article I show some examples of this use.

What is the difference between JSON and jQuery?

Json: JSON is a text format that is completely language independent. JQuery:It is a fast and minified JavaScript Library that simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development. jQuery is designed to change the way that you write JavaScript.


1 Answers

Here it is:

<script type='text/javascript'>  function xmlToString(xmlData) {       var xmlString;     //IE     if (window.ActiveXObject){         xmlString = xmlData.xml;     }     // code for Mozilla, Firefox, Opera, etc.     else{         xmlString = (new XMLSerializer()).serializeToString(xmlData);     }     return xmlString; }     </script> 

Taken from here

like image 159
Yarin Avatar answered Sep 28 '22 12:09

Yarin