pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/violentmonkey/xml-parser

sorigen="anonymous" media="all" rel="stylesheet" href="https://github.githubassets.com/assets/repository-3aa75d4cd2374bb7.css" /> GitHub - violentmonkey/xml-parser: Lightweight XML to JSON parser with zero dependencies · GitHub
Skip to content

Repository files navigation

@violentmonkey/xml-parser

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

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 (number)

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 | number | { [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/number; elements with children return object.
Arrays Repeated sibling elements are automatically wrapped in an array.
Numbers Numeric strings like "1234" are parsed as numbers.
CDATA CDATA sections are treated as text content.
Warnings Non-fatal issues (e.g. duplicate attributes) are returned in warnings array.
Zero dependencies ~250 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

pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy