Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including xml2o with all npm packages installed. Try it out:

var xml2o = require("xml2o")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

xml2o v0.3.2

Helps you convert XML into an object for easy reading.

xml2o

Helps you convert XML into an object for easy reading.

Getting Started

Install the package:

npm i -S xml2o

Let's convert XML from stream:

import {convertStream} from 'xml2o';
import {createReadStream} from 'fs';

const node = convertStream(createReadStream('/path/to/file.xml'));

We can doing same with string:

import {convertString} from 'xml2o';

const node = convertString('<node><foo bar="bar">foo</foo></node>');

Examples

A SimpleXML-like Node object made to help you read XML structures in JS without DOM.

Check a node

import {convertString} from 'xml2o';

const xml = `<node>
    <foo bar="bar">foo</foo>
    <list>
        <baz id="1" name="baz 1" />
        <baz id="2" name="baz 2" />
        <baz id="3" name="baz 3" />
    </list>
</node>`;

const node = convertString(xml);
console.log(node);

Root of a node, name and inner text

console.log(
    node.name,
    node.text
);

Child node name, text and attributes

console.log(
    node[0].name,
    node[0].text,
    node[0].getAttribute('bar'),
    node[0].getAttributeNode('bar'),
    node[0].getAttributes()
)

Node children

console.log(...node.map(child => child.name));

Node query

import {convertString} from 'xml2o';

const xml = `<node>
    <a/>
    <b>
        <a/>
        <a/>
        <c><a/></c>
    </b>
    <d>
        <c><a/></c>
    </d>
</node>`;

const node = convertString(xml);
console.log(node.query('/a')); // found /node/a
console.log(node.query('a')); // found /node/a, /node/b/a, /node/b/c/a, /node/d/c/a
console.log(node.query('c/a')); // found /node/b/c/a, /node/d/c/a
console.log(node.query('/d/c')); // found /node/d/c
console.log(node.query('b/a')); // found /node/b/a

Documentation

MethodArgumentsReturnDescription
convertStringXMLStringNodeXML string
convertStreamstreamNodeReadable stream

Node

Node class used to present XML nodes as objects. Every Node object has following properties and methods:

Properties

PropertyDescription
nameTag name
localTag local name
prefixTag prefix
parentParent Node
rootRoot Node

Methods

MethodArgumentsReturnDescription
getAttributename, uri?stringReturns an attribute value
getAttributeNodename, uri?AttributeReturns an attribute
getAttributesArray<string>Returns an array of attributes values
hasAttributename, uri?booleanReturns true if an attribute is exists
queryname, uri?Array<Node>Returns matched nodes in any level

Note

Code examples written with modules so you may need babel, typescript or other to run its or rewrite ES6 imports to:

const createString = require('xml2o').createString;

This library written in ES6 and if you need ES3 build you can tell me i'll make support for older JS versions.

License

MIT

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free