Socket
Socket
Sign inDemoInstall

simple-xml-builder

Package Overview
Dependencies
0
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    simple-xml-builder

A simple node package for building large XML files


Version published
Weekly downloads
1
Maintainers
1
Created
Weekly downloads
 

Readme

Source

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

FieldTypeRequiredDefault
outputFilestringtrue
nameSpacestring|undefined|nullfalse
buffSizenumberfalse50000
formatbooleanfalsefalse
delimiterstringfalse

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

FieldType
keystring
valuestring

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

FieldTypeRequiredDefault
tagstringtrue
selfClosingbooleanfalsefalse
attributesAttribute[]false[]
processingbooleanfalsefalse

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

Keywords

FAQs

Last updated on 24 Jan 2023

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc