xml2jsobj
Convert XML to JS object and maintain the order
Last updated 4 years ago by kong0107 .
ISC · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install xml2jsobj 
SYNC missed versions from official npm registry.

xml2jsobj

Convert XML to JS object asynchronously and maintain the order.

Install

npm install xml2jsobj

Example

require("xml2jsobj").convert(
    '<r>\
        <a>A1</a>\
        <b c="d">B</b>\
        <a>\
            A2\
            <e />\
        </a>\
    </r>',
    function(err, result) {
        console.dir(result);
    }
);

Output

{   "name": "R",
    "children": [
        {   "name": "A",
            "children": ["A1"]
        },
        {   "name": "B",
            "attributes": {"C": "d"},
            "children": ["B"]
        },
        {   "name": "A",
            "children": [
                "A2",
                {"name": "E"}
            ]
        }
    ]
}

Idea: just be stupid

  • There are at most three member for each node: name, attributes, children.
  • Maintain the order of the children. In the example above, the <a> tags are NOT combined as an array (which most other packages such as xml2js and its dependents do), and therefore the result object still knows that the second <a> tag is after the <b> tag.
  • Even with only one child, the node would still have a children member as an array.

Usage

Call <obj>.init for initialization if you need. The arguments are just like that for sax's parser, except that trim is default to true.

Current Tags

  • 0.1.6                                ...           latest (4 years ago)

7 Versions

  • 0.1.6                                ...           4 years ago
  • 0.1.5                                ...           4 years ago
  • 0.1.4                                ...           4 years ago
  • 0.1.3                                ...           4 years ago
  • 0.1.2                                ...           4 years ago
  • 0.1.1                                ...           4 years ago
  • 0.1.0                                ...           4 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 7
Last Day 0
Last Week 0
Last Month 0
Dependencies (1)
Dev Dependencies (0)
None
Dependents (1)

Copyright 2014 - 2016 © taobao.org |