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

simple-xml-builder

v1.0.3

Published

A simple node package for building large XML files

Downloads

5

Readme

simple-xml-builder

A simple node package for building large xml files.

Installation

$ npm install simple-xml-builder

Usage

To import the package we can do the following

//CJS
const { XMLBuilder } = require("simple-xml-builder");

//ESM
import { XMLBuilder } from "simple-xml-builder";

Here we have a simple example

import { XMLBuilder } from "simple-xml-builder";

const xml = new XMLBuilder({ outputFile: "example1.xml", format: true });

xml.add({ tag: "cars" })
    .add({ tag: "ford" })
    .add({ tag: "seats" })
    .txt("4")
    .add({ tag: "horsepower" })
    .txt("349")
    .end(); // or .close().close()

And it's corresponding output file

<cars>
  <ford>
    <seats>
      4
    </seats>
    <horsepower>
      349
    </horsepower>
  </ford>
</cars>

A more complex example

import { XMLBuilder } from "simple-xml-builder";

const xml = new XMLBuilder({ outputFile: "example2.xml", format: true });
xml.add({ tag: "xml", attributes: [{ key: "version", value: "1.0" }, { key: "encoding", value: "UTF-8" }], processing: true })
    .setNamespace("fb")
    .add({ tag: "post", attributes: [{ key: "id", value: "1" }] })
    .setNamespace(null)
    .add({ tag: "user" })
    .txt("John Doe")
    .add({ tag: "content" })
    .txt("hello")
    .close()
    .setNamespace("ig")
    .add({ tag: "post" })
    .setNamespace(null)
    .add({ tag: "user" })
    .txt("John Smith")
    .add({ tag: "keywords" })
    .add({ tag: "Travel", selfClosing: true })
    .add({ tag: "Photography", selfClosing: true })
    .end();

Output

<?xml version="1.0" encoding="UTF-8"?>
<fb:post id="1">
  <user>
    John Doe
  </user>
  <content>
    hello
  </content>
</post>
<ig:post>
  <user>
    John Smith
  </user>
  <keywords>
    <Travel/>
    <Photography/>
  </keywords>
</post>

API

XMLBuilder(options)

The XMLBuilder class takes an options parameter with the following format

Field | Type | Required | Default :---:|:---:|:---:|:---: outputFile | string | true | nameSpace | string|undefined|null | false | buffSize | number | false | 50000 format | boolean | false | false delimiter | string | false |

The outputFile is used to specify the path of the generated XML file. nameSpace is used to specify the initial namespace of the tags, it can be later modified ussing .setNamespace(nameSpace). The class is writing the file content synchronous using a buffer, for peformance reasons, buffSize is used to specify the size of the buffer (in lines). format is used to specify if we want our genereated xml to be formatted, keeping this off will give a smaller output file.

Attribute

Attribute for XML tags, they have the following format

Field | Type :---:|:---: key | string value | string

.add(options)

The add function is used to add a new tag to the file, it receives one parameter with the following format. The value will be XML encoded.

Field | Type | Required | Default :---:|:---:|:---:|:---: tag | string | true | selfClosing | boolean | false | false attributes | Attribute[] | false | [] processing | boolean | false | false

The selfClosing parameters specifies if the tag should be self closing, e.g

<someTag/>

The attributes option takes an array of Attributes and adds them to the tag. processing is used to specify if the tag is a processing instruction, e.g

<? ... ?>

.txt(content: string)

The txt function is used to add text content inside a tag. After a call to this function, the current tag will be closed automatically

.close()

The close function is used to close the current opened tag.

.end()

The end function is used to close all current opened tags.

.log()

The log function can be used for debugging, it will console.log the current buffer, opened tags and indentation level