XML node tree
Last updated 4 years ago by maleo1 .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install xml-node 
SYNC missed versions from official npm registry.

Xml Node

Converts XML to and from JS node tree.

Source XML

<root><image src="image.jpg"/><content>Text <image src="image2.jpg"/> <![CDATA[&#xad;]]></content></root>

As a JS Node

var Parser = require("xml-node").XmlParser;
Parser.toNode(srcxml, function(err, node){
  var content = node.getChild("root").getChild("content");
  // Get text from node, returns: "Text &#xad;"
  // Get an attribute of a node, returns: "image2.jpg"

Raw Object

{ "value": [
  { "name": "root", "value": [
    { "name": "image", "attributes": { "src": "image.jpg" } },
    { "name": "content", "value": [
      { "text": "Text " }
      { "name": "image", "attributes": { "src": "image2.jpg" }},
      { "cdata": "&#xad;" }


Converts among XmlNode / XmlNode object / XML.

var Parser = require("xml-node").XmlParser;

Parser.toNode(input, [options], callback])

Converts an XML or a raw object into a node.

Parser.toXml(input, [options], callback)

Converts a raw object or node into an XML string

Parser.toObject(input, [options], callback)

Converts an XML or a node into a raw object


Creating a node

var name = "foo", attr: { bar: "baz" }, parent = new XmlNode(); // Empty node;

// argument "attributes" and "parent" are optional
var node = new XmlNode(name, attr, parent);

var nodeFromObject = new XmlNode({ name: name, attributes: attr });

//<foo bar="baz"><foo bar="baz"/></foo>

addInstruction(name, body)

addComment(comment, [index])

addCData(cdata, [index])

addText(text, [index])

  • text: string

addChild(child, [index])

Adds a child to the current node. The child must not be the node itself.

  • child: a raw object, an instance of XmlNode, or the name of the new node.


  • query: either string, number, or object
    • string: Name of the child node. Remove the first child with same node name
    • number: Index of the child node. Remove the child at the indicated index
    • object: Remove the child with the same property: node.removeChild({name: "foo", attributes: { bar: "baz" }}) removes the child of node which has the name foo and attribute bar equaling to baz


Removes the first child node found using query

  • query: same as getChild()

insert(node, [before])

Inserts the node as the next sibling of the current node.

  • node: either raw object or an instance of XmlNode
  • before: boolean
    • insert as the previous sibling the current node instead of the next.


Removes the current node from the node tree.


Tell if the current node is an empty node.


Tell if the current node is the root node.


Tell if the current node is a leaf node, i.e. no descendants.


Gets the index of the current node among the children of the parent node.


Get all attributes of the current node.


Gets an attribute of the current node.

  • name: string

setAttribute(name, attribute)

Sets an attribute to the current node.

  • name: string
  • attribute: string


Removes an attribute from the current node.

  • name: string


Gets the children of the current node.

  • query: function, number or object


  • Gets all ancestors matching the query, all ancestors if there's no query.
  • query: string, function, or object


Gets all the descendants of the current node according to the query

  • query: string, function, or object


Gets the previous sibling of the current node.


Gets the next sibling of the current node.


Gets the parent of the current node.


Gets the root node of the node tree


Gets the path of the current node from the root node

  • .: Indicates the root node
  • -: Indicates an empty node
  • child nodes are in the form of {index}:{name}[i] or {index}:#{type},
    • index is the index of the node among its siblings.
    • name is the name of the node
    • i is the index of the node with the same name among its siblings.
    • #{type} is type of the node, either #text, #cdata, or #comment.
    • E.g. 5:node[2] means the 5th node is a node node, and the 3rd node node.


Gets the name of the current node.


Returns a string of all text content of the current node and its descendants.


Converts the node to a raw object.


Converts the whole node tree to an XML string.


Converts the node tree with the current node as the root to an XML fragment string.

Current Tags

  • 0.0.1                                ...           latest (4 years ago)

1 Versions

  • 0.0.1                                ...           4 years ago
Maintainers (1)
Today 0
This Week 1
This Month 1
Last Day 0
Last Week 0
Last Month 2
Dependencies (3)
Dev Dependencies (4)
Dependents (0)

Copyright 2014 - 2016 © taobao.org |