/xml-parser

jQuery powered XML parser for client side JavaScript

Primary LanguageJavaScript

jQuery powered XML parser for client side JavaScript

This lightweight XML parser provides straightforward parsing and marshalling for well formed XML documents using jQuery. The usage is almost the same as when dealing with HTML DOM.

The script defines the XMLParser object, which provides the parse and stringify methods, just like the JavaScript JSON object.

##Sample usage

Suppose you have the following XML document.

<?xml version="1.0" encoding="UTF-8"?>
<team>
  <member>
    <name>Oswaldo</name>
    <language>JavaScript</language>
  </member>
  <member>
    <name>Irving</name>
    <language>Scala</language>
  </member>
</team>

Use parse method to get a jQuery representation.

var $xml = XMLParser.parse(xml);

Edit some data.

//just use $xml as query context!
$('member[name=Oswaldo]', $xml).attr('language', 'C++');

Add elements.

$('team', $xml).append('<member><name>Andree</name><language>Java</language></member>');

Output to XML string.

xml = XMLParser.stringify($xml);

Result.

<?xml version="1.0" encoding="UTF-8"?>
<team>
  <member>
    <name>Oswaldo</name>
    <language>C++</language>
  </member>
  <member>
    <name>Irving</name>
    <language>Scala</language>
  </member>
  <member>
    <name>Andree</name>
    <language>Java</language>
  </member>
</team>