xml-parser-xo
Parse a XML string into a proprietary syntax tree
Last updated a year ago by chrisbottin .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install xml-parser-xo 
SYNC missed versions from official npm registry.

xml-parser-xo

An XML parser based on xml-parser.

Build Status npm version

Installation

$ npm install xml-parser-xo

Example

Usage:

import xmlParser from 'xml-parser-xo';

var xml = `<?xml version="1.0" encoding="utf-8"?>
<!-- Load the stylesheet -->
<?xml-stylesheet href="foo.xsl" type="text/xsl" ?>
<!DOCTYPE foo SYSTEM "foo.dtd">
<foo><![CDATA[some text]]> content</foo>`;

xmlParser(xml);

Output:

{
    "declaration": {
        "type": "ProcessingInstruction",
        "attributes": {"version": "1.0", "encoding": "utf-8"}
    },
    "root": {
        "type": "Element",
        "name": "foo",
        "attributes": {},
        "children": [
            {"type": "CDATA", "content": "<![CDATA[some text]]>"},
            {"type": "Text", "content": " content"}
        ]
    },
    "children": [
        {"type": "Comment", "content": "<!-- Load the stylesheet -->"},
        {"type": "ProcessingInstruction", "attributes": {"href": "foo.xsl", "type": "text/xsl"}},
        {"type": "DocumentType", "content": "<!DOCTYPE foo SYSTEM \"foo.dtd\">"},
        {
            "type": "Element",
            "name": "foo",
            "attributes": {},
            "children": [
                {"type": "CDATA", "content": "<![CDATA[some text]]>"},
                {"type": "Text", "content": " content"}
            ]
        }
    ]
}

Options

  • filter: Function to filter out unwanted nodes by returning false.
    • type: function(node) => boolean
    • default: () => true
  • strictMode: True to throw an error when parsing XML document with invalid content like mismatched closing tags.
    • type: boolean
    • default: false

Usage:

import xmlParser from 'xml-parser-xo';

const xml = `<?xml version="1.0" encoding="utf-8"?>
<!-- Load the stylesheet -->
<?xml-stylesheet href="foo.xsl" type="text/xsl" ?>
<!DOCTYPE foo SYSTEM "foo.dtd">
<foo><![CDATA[some text]]> content</foo>`;

xmlParser(xml, {
    filter: (node) => {
        return node.type === 'Element' || node.type === 'Text';
    }
});

Output:

{
    "declaration": {
        "type": "ProcessingInstruction",
        "attributes": {"version": "1.0", "encoding": "utf-8"}
    },
    "root": {
        "type": "Element",
        "name": "foo",
        "attributes": {},
        "children": [
            {"type": "Text", "content": " content"}
        ]
    },
    "children": [
        {
            "type": "Element",
            "name": "foo",
            "attributes": {},
            "children": [
                {"type": "Text", "content": " content"}
            ]
        }
    ]
}

License

MIT

Current Tags

  • 4.1.1                                ...           latest (a year ago)

19 Versions

  • 4.1.1                                ...           a year ago
  • 4.1.0                                ...           a year ago
  • 4.0.5                                ...           2 years ago
  • 4.0.4                                ...           2 years ago
  • 4.0.3                                ...           2 years ago
  • 4.0.2                                ...           2 years ago
  • 4.0.1                                ...           2 years ago
  • 4.0.0                                ...           2 years ago
  • 3.2.0                                ...           3 years ago
  • 3.1.2                                ...           3 years ago
  • 3.1.1                                ...           4 years ago
  • 3.1.0                                ...           4 years ago
  • 3.0.1                                ...           4 years ago
  • 3.0.0                                ...           5 years ago
  • 2.2.1                                ...           5 years ago
  • 2.2.0                                ...           5 years ago
  • 2.1.3                                ...           8 years ago
  • 2.1.2                                ...           8 years ago
  • 2.1.1                                ...           8 years ago
Maintainers (1)
Downloads
Total 0
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (10)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE