New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

@styleshit/xml-parser

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@styleshit/xml-parser

Simple XML to AST parser in TypeScript

latest
Source
npmnpm
Version
0.0.1
Version published
Maintainers
1
Created
Source

XML Parser

Simple XML to AST parser in TypeScript.

This parser has a custom grammar and exists for learning purposes.

Features

The parser supports elements (''), attributes (''), and text nodes ('text').

It does not support comments, self-closing tags, or other XML features.

Installation

npm install @styleshit/xml-parser

Usage

To use it, import the parse function and pass an XML string to it.

For example, this code:

import { parse } from '@styleshit/xml-parser';

const ast = parse(`
    <root>
        <element attribute="value">first text</element>
    </root>
    <sibling>another text</sibling>
`);

console.log(ast);

Will output:

const ast = {
  kind: 'document',
  children: [
    {
      kind: 'element',
      name: 'root',
      children: [
        {
          kind: 'element',
          name: 'element',
          children: [
            {
              kind: 'text',
              text: 'first text',
            },
          ],
          attributes: {
            attribute: 'value',
          },
        },
      ],
      attributes: {},
    },
    {
      kind: 'element',
      name: 'sibling',
      children: [
        {
          kind: 'text',
          text: 'another text',
        },
      ],
      attributes: {},
    },
  ],
};

While this code will throw an error:

import { parse } from '@styleshit/xml-parser';

const ast = parse('<element>text</another-element>');

// SyntaxError: Expected closing tag for 'element' at index 13. Got 'another-element' instead.

FAQs

Package last updated on 25 Jan 2025

Did you know?

Socket

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