Convert XML to JSON

Convert JSON to XML

This is based on the BadgerFish rules, but for a effective translation, new rules had to be added.

The used rules:

  • Element names become object properties
  • Text content of elements goes in the $ property of an object.
  • Nested elements become nested properties
  • Multiple elements at the same level become array elements.
  • Attributes go in properties whose names begin with @.
  • Active namespaces for an element go in the element's @xmlns property.
  • The default namespace URI goes in @xmlns.$.
  • Other namespaces go in other properties of @xmlns.
  • Elements with namespace prefixes become object properties, too.
  • The @xmlns propertie goes only in object relative to the TAG where namespace was declared.
  • Text fragments in mixed contents (TAGs and text) goes in properties named $1, $2, etc.
  • Comment TAGs , like the text fragments, goes in properties named !1, !2, etc.
  • CDATA sections goes in properties named #1, #2, etc.

What is broken or left to do

  • A method to store the data in TAGs outside the root TAG
  • If the inner text of a TAG is entirely whitespace, it will be ignored

Credits

json.js -> modified by Cau Guanabara starting from this script
badgerfish.js and xmlparser.js -> developed by Cau Guanabara

Cau Guanabara - caugbr(a)gmail.com