0.4.13 • Published 2 years ago

xml2o v0.4.13

Weekly downloads
90
License
MIT
Repository
github
Last release
2 years ago

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

0.4.10

2 years ago

0.4.9

2 years ago

0.4.8

2 years ago

0.4.13

2 years ago

0.4.11

2 years ago

0.4.12

2 years ago

0.4.5

2 years ago

0.4.4

2 years ago

0.4.7

2 years ago

0.4.6

2 years ago

0.4.1

2 years ago

0.4.3

2 years ago

0.4.0

2 years ago

0.3.3

5 years ago

0.3.2

6 years ago

0.3.1

6 years ago

0.2.0

8 years ago

0.1.5

8 years ago

0.1.4

8 years ago

0.1.3

8 years ago

0.1.2

8 years ago

0.1.1

8 years ago