Skip to content

Instantly share code, notes, and snippets.

@hbi99
Last active January 1, 2016 04:28
Show Gist options
  • Save hbi99/8091760 to your computer and use it in GitHub Desktop.
Save hbi99/8091760 to your computer and use it in GitHub Desktop.
var str = '<data><user id="1" name="Hakan Bilgin">'+
'<mail>hbi@bredband.net</mail></user></data>',
doc = Defiant.xmlFromString(str),
obj = Defiant.node.toJSON(doc.documentElement);
console.log( JSON.stringify(obj, null, '\t') );
/*
{
"user": {
"@id": 1,
"@name": "Hakan Bilgin",
"mail": "hbi@bredband.net"
}
}
*/
// Changing it back to XML
var doc2 = JSON.toXML( obj );
console.log( Defiant.node.prettyPrint( doc2 ) );
/*
<?xml version="1.0" encoding="UTF-8"?>
<d:data xmlns:d="defiant-namespace>
<user id="1" name="Hakan Bilgin">
<mail>hbi@bredband.net</mail>
</user>
</d:data>
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment