xml2json

Public function that parses XML and returns JSON. It hides all errors in
Document structure, so the result may be not as strict as expected.
The function converts XML string or node to an object with attributes
introduced as same named string properties. All subnodes are also converted to
correspondingly named properties by the next rules:
- if there is only one child with this name, it is converted into an object
an added just as an object property to the parent object.
- if there are multiple children with the same name, they are also converted
to a separate objects and then added to the parent object as an array.
- if there is an attribute named just like the node(s), the node(s) will
completely replace the attribute.
- if there is any text value or a CDATA node or any their combination in the
node, that value is stored as a string in a property named "value" even in
cases when there is a subnode named "value" (but the node with this name
still can be found in "#children#" array mentioned below).
Also some service properties are added to an object representing a node:
- "#name#" property - the node name
- "#children#" array property - all node children listed in order they appear
in the node
- mentioned above "value" string property - contains textual value of the
node
Usage sample:
var a = new XMLHttpRequest();
a.open('GET', 'https://sample.com', false, "", "");
a.send(null);
// for example there was the next XML:
//
//
// some text value
//
//
var json = anychart.utils.XML2JSON(a.responseXML);
json variable will have the following structure:
{
#name#: "root",
#children#: [
{#name#: "child", #children#: []},
{#name#: "child", #children#: [], value: "some text value and a CDATA "}
{#name#: "child_other", #children#: []}
],
a: "123",
child: [
{#name#: "child", #children#: []},
{#name#: "child", #children#: [], value: "some text value and a CDATA "}
],
child_other: {#name#: "child_other", #children#: []}
}