Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

js-parse-xml

Package Overview
Dependencies
Maintainers
0
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

js-parse-xml

lightweight Node-JS xml parser

  • 2.0.6
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
89
increased by111.9%
Maintainers
0
Weekly downloads
 
Created
Source

🌟 js-parse-xml

Coverage Status NPM Downloads ISC License Discord

🚀 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.

Keywords

FAQs

Package last updated on 18 Aug 2024

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc