Following functions are used to process collections of child or descendant nodes.

each

Applies a callback function to child nodes of the current node. Returns the current node.

each(callback: (node: XMLBuilder, index: number, level: number) => void, self?: boolean, recursive?: boolean, thisArg?: any)


  • callback - a callback function which receives each child node as its first argument, child node index as its second argument and child node level as its third argument
  • self - whether to visit the current node along with child nodes (optional)
  • recursive - whether to visit all descendant nodes in tree-order or only the immediate child nodes (optional)
  • thisArg - value to use as this when executing callback (optional)
const { create } = require('xmlbuilder2');

const root = create().ele('root');
root.ele('a').up()
    .ele('b').up()
    .ele('c').up();
root.each((n, i) => n.att('id', i + 1));
console.log(root.end({ prettyPrint: true }));
<?xml version="1.0"?>
<root>
  <a id="1"/>
  <b id="2"/>
  <c id="3"/>
</root>

map

Produces an array of values by transforming each child node with the given callback function.

map(callback: (node: XMLBuilder, index: number, level: number) => any, self?: boolean, recursive?: boolean, thisArg?: any)


  • callback - a callback function which receives each child node as its first argument, child node index as its second argument and child node level as its third argument
  • self - whether to visit the current node along with child nodes (optional)
  • recursive - whether to visit all descendant nodes in tree-order or only the immediate child nodes (optional)
  • thisArg - value to use as this when executing callback (optional)
const { create } = require('xmlbuilder2');

const root = create().ele('root');
root.ele('a').up()
    .ele('b').up()
    .ele('c').up();
const names = root.map(n => n.node.nodeName);
console.log(names); // ['a', 'b', 'c']

reduce

Reduces child nodes into a single value by applying the given callback function.

reduce(callback: (value: any, node: XMLBuilder, index: number, level: number) => any, initialValue: any, self?: boolean, recursive?: boolean, thisArg?: any)


  • callback - a callback function which receives the current value as its first argument, each child node as its second argument, child node index as its third argument and child node level as its fourth argument
  • initialValue - initial value
  • self - whether to visit the current node along with child nodes (optional)
  • recursive - whether to visit all descendant nodes in tree-order or only the immediate child nodes (optional)
  • thisArg - value to use as this when executing callback (optional)
const { create } = require('xmlbuilder2');

const root = create().ele('root');
root.ele('a').up()
    .ele('b').up()
    .ele('c').up();
const names = root.reduce((val, n) => val + n.node.nodeName, '');
console.log(names); // 'abc'

find

Returns the first child node satisfying the given predicate, or undefined if there are no child nodes that satisfy the predicate.

find(predicate: (node: XMLBuilder, index: number, level: number) => boolean, self?: boolean, recursive?: boolean, thisArg?: any)


  • predicate - a function which receives each child node as its first argument, child node index as its second argument and child node level as its third argument and returns a boolean value indicating whether the child node satisfies the predicate
  • self - whether to visit the current node along with child nodes (optional)
  • recursive - whether to visit all descendant nodes in tree-order or only the immediate child nodes (optional)
  • thisArg - value to use as this when executing callback (optional)
const { create } = require('xmlbuilder2');

const root = create().ele('root');
root.ele('a').up()
    .ele('b').up()
    .ele('c').up();
const bNode = root.find(n => n.node.nodeName === 'b');
console.log(bNode.node.nodeName); // 'b'

filter

Produces an array of child nodes which pass the given predicate test.

filter(predicate: (node: XMLBuilder, index: number, level: number) => boolean, self?: boolean, recursive?: boolean, thisArg?: any)


  • predicate - a function which receives each child node as its first argument, child node index as its second argument and child node level as its third argument and returns a boolean value indicating whether the child node satisfies the predicate
  • self - whether to visit the current node along with child nodes (optional)
  • recursive - whether to visit all descendant nodes in tree-order or only the immediate child nodes (optional)
  • thisArg - value to use as this when executing callback (optional)
const { create } = require('xmlbuilder2');

const root = create().ele('root');
root.ele('node1').up()
    .txt('text')
    .ele('node2').up()
    .txt('more text');
const textNodes = root.filter(n => n.node.nodeType === 3); // contains 'text' and 'more text' nodes

every

Returns true if all child nodes pass the given predicate test.

every(predicate: (node: XMLBuilder, index: number, level: number) => boolean, self?: boolean, recursive?: boolean, thisArg?: any)


  • predicate - a function which receives each child node as its first argument, child node index as its second argument and child node level as its third argument and returns a boolean value indicating whether the child node satisfies the predicate
  • self - whether to visit the current node along with child nodes (optional)
  • recursive - whether to visit all descendant nodes in tree-order or only the immediate child nodes (optional)
  • thisArg - value to use as this when executing callback (optional)
const { create } = require('xmlbuilder2');

const root = create().ele('root');
root.ele('node1').up()
    .ele('node2').up()
root.every(n => n.node.nodeName.startsWith('n')); // true

some

Returns true if any of the child nodes pass the given predicate test.

some(predicate: (node: XMLBuilder, index: number, level: number) => boolean, self?: boolean, recursive?: boolean, thisArg?: any)


  • predicate - a function which receives each child node as its first argument, child node index as its second argument and child node level as its third argument and returns a boolean value indicating whether the child node satisfies the predicate
  • self - whether to visit the current node along with child nodes (optional)
  • recursive - whether to visit all descendant nodes in tree-order or only the immediate child nodes (optional)
  • thisArg - value to use as this when executing callback (optional)
const { create } = require('xmlbuilder2');

const root = create().ele('root');
root.ele('node1').up()
    .ele('child').up()
    .ele('node2').up()
root.some(n => n.node.nodeName.startsWith('n')); // true

toArray

Produces an array of child nodes.

toArray(self?: boolean, recursive?: boolean)


  • self - whether to visit the current node along with child nodes (optional)
  • recursive - whether to visit all descendant nodes in tree-order or only the immediate child nodes (optional)
const { create } = require('xmlbuilder2');

const root = create().ele('root');
root.ele('a').up()
    .ele('b').up()
    .ele('c').up()
const nodes = root.toArray(); // contains nodes a, b and c