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

js-parse-xml

v2.0.6

Published

lightweight Node-JS xml parser

Downloads

184

Readme

🌟 js-parse-xml

NPM Downloads

🚀 Key Features

  • Stream based: Efficiently handles large files, tested up to 700MB.
  • 0 dependencies: A small package size with no external dependencies.
  • Fast Parsing: Optimized for speed.
  • Well-formed XML validation: Ensures the integrity of your XML.
  • Maintains tag order: Preserves the order of tags in the output object.
  • Customizable whitespace handling: Tailor whitespace parsing to your needs.
  • Type conversion: Optionally parse content into the correct data types.

🛠 Installation

Install using NPM or Yarn:

npm i --save js-parse-xml

OR

yarn add js-parse-xml

🌟 Getting Started

ESM Imports

import { Parser, parseString, parseStringSync, parseFile, parseFileSync } from "js-parse-xml";

CommonJS Imports

const { Parser, parseString, parseStringSync, parseFile, parseFileSync } = require("js-parse-xml");

📸 Example

Synchronous Parsing

let json = parseStringSync("<xml>Example!</xml>");
// do whatever with the json here
let json = parseFileSync("file.xml");
// do whatever with the json here

Asynchronous Parsing

async function parse(string) {
  let json = await parseString(string);
  // do whatever with the json here
}
parse("<xml>Testing</xml>");
async function parse(file) {
  let json = await parseFile(file, { stream: true });
  // do something with the json
}
parse("large_file.xml");

Using the Parser Class

// strict:false will continue parsing if it finds error
let parser = new Parser({ strict: false });

// create stream and feed each chunk to the parser
let stream = fs.createReadStream("filename.xml", "utf-8");

stream.on("data", parser.feed);

stream.on("end", () => {
  let json = parser.finish();
  // do whatever with the json
});

🌍 Supported Environments

  • Node.js
  • Browsers (with bundlers like Webpack or Rollup)

🔧 Output Examples

XML Input

<space:test>
    <example>
        content
    </example>
</space:test>

Parsed JSON Output

{
    "test": {
        "example": "content"
    }
}

XML Input

<test>
    <v>0.01e2</v>
    <v>0003</v>
    <v>-003</v> 
    <v>0x2f</v>
    <v><![CDATA[  <xml> ]]></v>
</test>

Parsed JSON Output

{
    "test": {
        "v": [1, 3, -3, 47, "<xml>"]
    }
}

🎨 Customization

Configure your parser with available options:

let options = {
    encoding: "utf-8",
    stream: false,
    preserve_whitespace: false,
    convert_values: true,
    strict: true
};

🔧 Acknowledgments & Contributions

The goal of this project is to be open source and community-driven. Therefore, contributing is welcomed, and any/all ideas and suggestions are taken into consideration. We welcome everyone to join our Discord server and post questions, comments, concerns, or feature requests! You can also navigate to our Github and open a new issue.