Socket
Socket
Sign inDemoInstall

xml-parser-xo

Package Overview
Dependencies
0
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    xml-parser-xo

Parse a XML string into a proprietary syntax tree


Version published
Weekly downloads
358K
decreased by-0.93%
Maintainers
1
Install size
41.9 kB
Created
Weekly downloads
 

Readme

Source

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

Keywords

FAQs

Last updated on 04 Jul 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc