Skip to content
This repository was archived by the owner on Nov 16, 2026. It is now read-only.
violentmonkeyPublic archive

About

Lightweight XML to JSON parser with zero dependencies

Resources

Stars

1 star

Watchers

1 watching

Forks

Repository files navigation

@violentmonkey/xml-parser

Open on npmx.dev Open on npmx.dev Open on npmx.dev

Note

Moved to @usync/xml-parser.

Lightweight XML to JSON parser with zero dependencies. Works in browser, Node.js, and Deno.

Designed for parsing XML APIs (WebDAV PROPFIND, S3 ListBucket, etc.) in environments where DOMParser is unavailable — such as service workers.

Usage

import { parseXml } from "@violentmonkey/xml-parser";

const { node, warnings } = parseXml(`<?xml version="1.0"?>
<D:multistatus xmlns:D="DAV:">
  <D:response>
    <D:href>/file.txt</D:href>
    <D:propstat>
      <D:prop>
        <D:displayname>file.txt</D:displayname>
        <D:getcontentlength>1234</D:getcontentlength>
      </D:prop>
      <D:status>HTTP/1.1 200 OK</D:status>
    </D:propstat>
  </D:response>
</D:multistatus>`);

// node.multistatus.response.href         → "/file.txt"
// node.multistatus.response.propstat.prop.displayname → "file.txt"
// node.multistatus.response.propstat.prop.getcontentlength → "1234" (string)

API

parseXml(input, options?)

Parameters:

  • input (string) — XML text to parse.
  • options (optional):
    • ignoreAttributes (boolean) — Skip all attributes. Default false.
    • removeNSPrefix (boolean) — Strip namespace prefixes from element names. Default true.

Returns: { node: Record<string, XmlValue>, warnings: Warning[] }

XmlValue

type XmlValue = string | { [key: string]: XmlValue } | XmlValue[];

Features

Feature Description
Namespace handling Strips D: → D:response becomes response by default. Opt out with removeNSPrefix: false.
Attributes Prefixed with @, e.g. <item id="42"> → { "@id": "42" }. xmlns* attributes are skipped.
Text vs object Text-only elements return string; elements with children return object.
Arrays Repeated sibling elements are automatically wrapped in an array.
Values Text is always returned as a string, e.g. "1234", "00123", "0x1F"; convert with Number() where needed.
DOCTYPE <!DOCTYPE ...> declarations (including internal subsets), comments, and processing instructions before the root are skipped.
CDATA CDATA sections are treated as text content.
Warnings Non-fatal issues (e.g. duplicate attributes) are returned in warnings array.
Zero dependencies ~300 lines of pure TypeScript.

License

MIT

About

Lightweight XML to JSON parser with zero dependencies

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Contributors

Languages