node-xml-parser

0.0.5 • Public • Published

node-xml-parser

Very simple and intuitive Node JS module that validates, parses and builds XML.


Validating XML

import { validateXML } from 'node-xml-parser'

const goodXML = '<document><text>Hello, World</text><symbol data="!"/></document>'
const badXML = '</document><text Hello, World><!/></doc>'

// Yes, it's as easy as that:

validateXML(goodXML) // > true
validateXML(badXML) // > false

Parsing XML

import { parseXML } from 'node-xml-parser'

const XML = '<document><text>Hello, World</text><symbol data="!"/></document>'

parseXML(XML)

/*
	Features clean, easily readable output:

	XMLNode {
		tag: 'document',
		attributes: Map(0) {},
		children: [
			XMLNode {
				tag: 'text',
				attributes: Map(0) {},
				children: [
					XMLNode {
						tag: '@text',
						attributes: Map(1) { data -> 'Hello, World' },
						children: []
					}
				]
			},
			XMLNode {
				tag: 'symbol',
				attributes: Map(1) { data -> '!' },
				children: []
			}
		]
	}
*/

Building XML

import { buildXML, XMLNode } from 'node-xml-parser'

// Wow, so readable!

const xml = new XMLNode('document', {}, [
	new XMLNode('text', {}, [
		'Hello, World'
	]),
	'!'
])

buildXML(xml, { indentationSize: 2, indentationType: 'spaces' })

/*
	Prettiest output you have ever seen:

	<document>
		<text>
			Hello, World
		</text>
		!
	</document>
*/

Options for buildXML:

interface XMLBuilderOptions {
	indentationSize: number             // Default: 1
	indentationType: 'tabs' | 'spaces'  // Default: 'tabs'
	seperator: string                   // Default: '\n'
	enableSelfClosingTags: boolean      // Default: true
}

/node-xml-parser/

    Package Sidebar

    Install

    npm i node-xml-parser

    Weekly Downloads

    2

    Version

    0.0.5

    License

    ISC

    Unpacked Size

    103 kB

    Total Files

    25

    Last publish

    Collaborators

    • iannisz