x2js demo
HTML
<script src="https://x2js.googlecode.com/hg/xml2json.js"></script>
<body>
<h1>X2JS Demo</h1>
<button id="convertToJsonBtn">XML => JSON</button>
<button id="convertToXmlBtn">JSON => XML</button>
<div>
<h4>XML:</h4>
<textarea id="xmlArea" cols="55" rows="15"></textarea>
</div>
<div>
<h4>JSON:</h4>
<textarea id="jsonArea" cols="55" rows="15"></textarea>
</div>
</body>
JavaScript
var convert = new X2JS(),
xmlStr = ['<article>',
'<section>',
'<h3>…</h3>',
'<textarea rich-text="true">',
'<p>Polaroid quis fingerstache twee <sup>+</sup>1 seitan XOXO chillwave. Post-ironic hella eu, <a href="www.pbr.com">PBR</a> readymade est Shoreditch selfies. Aliquip fashion axe leggings cardigan kitsch placeat.</p><p>Cred labore cornhole, hashtag church-key ugh you probably haven\'t heard of them PBR tofu twee irony street art heirloom tousled.</p>',
'</textarea>',
'</section>',
'<section>',
'<h3>…</h3>',
'<textarea rich-text="true">',
'<p>Vegan cornhole et Pitchfork laboris, raw denim readymade post-ironic lumbersexual mollit VHS cold-pressed Carles. Occupy migas tofu American Apparel fugiat. Sriracha umami brunch, post-ironic in Godard readymade assumenda cold-pressed anim typewriter Etsy before they sold out artisan.</p>',
'</textarea>',
'</section>',
'</article>'].join('');
function xml2json() {
$("#jsonArea")
.val(
JSON.stringify(
convert.xml_str2json(
$("#xmlArea").val()
)
)
);
}
function json2xml() {
$("#xmlArea").val(
convert.json2xml_str(
$.parseJSON(
$("#jsonArea").val()
)
)
);
}
$("#xmlArea").val(xmlStr);
xml2json();
json2xml();
$("#convertToJsonBtn").click(xml2json);
$("#convertToXmlBtn").click(json2xml);