Skip to content

Lightweight pure JavaScript XML parser that attempts to conforms to all REC-xml-19980210 standards

Notifications You must be signed in to change notification settings

JeremyMColegrove/js-parse-xml

Repository files navigation

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