npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tsl-core-xml

v0.4.0

Published

TypeScript Library for XML

Downloads

19

Readme

XML TypeScript Library

This package provides simple ways to parse any XML-like text.

CodeQL Node.js NPM License

Table of Content

XMLTree

The XML tree, often also called the DOM (Document Object Model), is the natural representation of XML. The XMLTree class can have multiple root nodes in the XMLTree.roots property.

Usually the last root is the one that contains most data. Or you check each root if it is a tag by using the isXMLTag helper function. Afterwards you can check the XMLTag.innerXML property for child nodes.

You can also use the XMLTag.query function to extract XML nodes with the help of selectors as known from CSS. It depends on the selector and use case whether this is faster than a custom walk through the tree nodes.

The XMLTree uses the XMLScanner, which is available via the XMLTree.scanner property. There you can adjust the XMLScanner.cdataTags property or the XMLScanner.scanSize property for special use cases.

XMLTree Example

const tree = XMLTree.parse(
    '<!DOCTYPE html>' +
    '<html lang="en"><head><title>My Webpage</title></head>' +
    '<body style="background:#9CF"><h1>My Webpage</h1><hr /></body></html>'
);

console.log( JSON.stringify( tree.query('body h1') );
console.log( JSON.stringify( tree.roots, null, '  ' ) );
{
  "tag": "h1",
  "innerXML": [
    "My Webpage"
  ]
}
[{
  "tag": "!DOCTYPE",
  "attributes": {
    "html": ""
  }
}, {
  "tag": "html",
  "attributes": {
    "lang": "en"
  }
  "innerXML": [{
    "tag": "head",
    "innerXML": [{
      "tag": "title",
      "innerXML": [
        "My Webpage"
      ]
    }]
  }, {
    "tag": "body",
    "attributes": {
      "style": "background:#9CF"
    },
    "innerXML": [{
      "tag": "h1",
      "innerXML": [
        "My Webpage"
      ]
    }, {
      "tag": "hr",
      "empty": true
    }]
  }]
}]

XMLScanner

If XML should be read exactly like it is written, the XMLScanner is the class to use. It keeps every linebreak and every variant of a closing tag. The only things not preserved by the scanner are the surrounding quote characters for attribute values.

If you expect text between XML tags or an XML tag itself to be larger than 1 MB, then you should increase the value of the XMLScanner.scanSize property accordingly. If you like to save memory during a scan, you can also decrease the scan size.

XMLScanner Example

const scanner = new XMLScanner(
    '<!DOCTYPE html>' +
    '<html lang="en"><head><title>My Webpage</title></head>' +
    '<body style="background:#9CF"><h1>My Webpage</h1><hr /></body></html>'
);

let node: ( XMLNode | undefined );

while ( node = scanner.scan() ) {
    console.log( node );
}
{ tag: "!DOCTYPE", attributes: { html: "" } }
{ tag: "html", attributes: { lang: "en" } }
{ tag: "head" }
{ tag: "title" }
"My Webpage"
{ tag: "/title" }
{ tag: "/head" }
{ tag: "body", attributes: { style: "background:#9CF" } }
{ tag: "h1" }
"My Webpage"
{ tag: "/h1" }
{ tag: "hr", empty: true }
{ tag: "/body" }
{ tag: "/html" }

Types of XML Nodes

XML has 7 different types of nodes.

  • string: Text strings are the regular escaped text between tags. Use the isString helper function to test for this node type.

  • XMLCdata: Character data is unescaped text, that contains raw data like JavaScript. Use the isXMLCdata helper function to test for this node type.

  • XMLComment: A comment is a special form of tag, that has not impact on the content of the XML. Use the isXMLComment helper function to test for this node type.

  • XMLTag: Tags are nodes with a name and attributes. Use the isXMLTag helper function to test for this node type. There exists 4 subtypes of tags.

    • Empty Tag: This tag is self-closing and has the XMLTag.empty property set to true. Typical empty tags are img, meta, and path.

    • Regular Tag: This tag often contains child nodes in the XMLTag.innerXML property. Typical regular tags are a, p, and text.

    • Document Type Definition: This tag is similar to a regular tag, but has a name starting with a ! character. A typical definition tag is !DOCTYPE.

    • Processing Instruction: This tag is similar to an empty tag, but has a name starting with a ? character. A typical instruction tag is ?xml.

XMLPrinter Example

const printer = new XMLPrinter( [
    { tag: "!DOCTYPE", attributes: { html: "" } },
    { tag: "html", attributes: { lang: "en" } }
]);

console.log( printer.toString() );
<!DOCTYPE html=""><html></html>