XMLBuilder
Represents a wrapper around XML nodes to implement easy to use and chainable document builder methods.
Interface XMLBuilder
interface XMLBuilderRepresents a wrapper around XML nodes to implement easy to use and chainable document builder methods.
Properties
node
NodeReturns the underlying DOM node.
options
XMLBuilderOptionsGets the options used while creating an XML document.
Methods
att
att(namespace: string | null, name: string, value: string): XMLBuilderCreates or updates an element attribute.
Parameters
namespace: string | nullnamespace of the attributename: stringattribute namevalue: stringattribute value
Returns XMLBuilder
current element node
att(name: string, value: string): XMLBuilderCreates or updates an element attribute.
Parameters
name: stringattribute namevalue: stringattribute value
Returns XMLBuilder
current element node
att(obj: AttributesObject): XMLBuilderCreates or updates an element attribute.
Parameters
obj: AttributesObjecta JS object containing element attributes and values
Returns XMLBuilder
current element node
com
com(content: string): XMLBuilderCreates a new comment node and appends it to the list of child nodes.
Parameters
content: stringnode content
Returns XMLBuilder
current element node
dat
dat(content: string): XMLBuilderCreates a new CDATA node and appends it to the list of child nodes.
Parameters
content: stringnode content
Returns XMLBuilder
current element node
dec
dec(options: { encoding?: string; standalone?: boolean; version?: "1.0" }): XMLBuilderUpdates XML declaration.
Parameters
options: { encoding?: string; standalone?: boolean; version?: "1.0" }declaration options
Returns XMLBuilder
current element node
doc
doc(): XMLBuilderReturns the document node.
Returns XMLBuilder
dtd
dtd(options?: DTDOptions): XMLBuilderCreates a new DocType node and inserts it into the document. If the document already contains a DocType node it will be replaced by the new node. Otherwise it will be inserted before the document element node.
Parameters
options?: DTDOptionsDocType identifiers
Returns XMLBuilder
current element node
each
each(callback: (node: XMLBuilder, index: number, level: number) => void, self?: boolean, recursive?: boolean, thisArg?: any): XMLBuilderTraverses through the child nodes of a node. callback is called with three
arguments: (node, index, level).
Parameters
callback: (node: XMLBuilder, index: number, level: number) => voida callback function to apply to each child nodeself?: booleanwhether to visit the current node along with child nodesrecursive?: booleanwhether to visit all descendant nodes in tree-order or only the immediate child nodesthisArg?: anyvalue to use as this when executing callback
Returns XMLBuilder
ele
ele(namespace: string | null, name: string, attributes?: AttributesObject): XMLBuilderCreates a new element node and appends it to the list of child nodes.
Parameters
namespace: string | nullelement namespacename: stringelement nameattributes?: AttributesObjecta JS object with element attributes
Returns XMLBuilder
the new element node
ele(name: string, attributes?: AttributesObject): XMLBuilderCreates a new element node and appends it to the list of child nodes.
Parameters
name: stringelement nameattributes?: AttributesObjecta JS object with element attributes
Returns XMLBuilder
the new element node
ele(obj: ExpandObject): XMLBuilderCreates new element nodes from the given JS object and appends it to the list of child nodes.
Parameters
obj: ExpandObjecta JS object representing nodes to insert
Returns XMLBuilder
the last top level element node created
ele(content: string): XMLBuilderCreates new element nodes from the given XML document string appends it to the list of child nodes.
Parameters
content: stringan XML document string representing nodes to insert
Returns XMLBuilder
the last top level element node created
end
end(): stringConverts the entire XML document into an XML document string with the default writer options.
Returns string
end(writerOptions: XMLWriterOptions): stringConverts the entire XML document into an XML document string.
Parameters
-
writerOptions: XMLWriterOptions
Returns string
end(writerOptions: JSONWriterOptions): stringConverts the entire XML document into a JSON string.
Parameters
-
writerOptions: JSONWriterOptions
Returns string
end(writerOptions: YAMLWriterOptions): stringConverts the entire XML document into a YAML string.
Parameters
-
writerOptions: YAMLWriterOptions
Returns string
end(writerOptions: ObjectWriterOptions): XMLSerializedAsObject | XMLSerializedAsObjectArrayConverts the entire XML document into its object representation.
Parameters
-
writerOptions: ObjectWriterOptions
Returns XMLSerializedAsObject | XMLSerializedAsObjectArray
end(writerOptions: MapWriterOptions): XMLSerializedAsMap | XMLSerializedAsMapArrayConverts the entire XML document into its object representation using ES6 maps.
Parameters
-
writerOptions: MapWriterOptions
Returns XMLSerializedAsMap | XMLSerializedAsMapArray
every
every(predicate: (node: XMLBuilder, index: number, level: number) => boolean, self?: boolean, recursive?: boolean, thisArg?: any): booleanReturns true if all child nodes pass the given predicate test.
predicate is called with three arguments: (node, index, level).
Parameters
predicate: (node: XMLBuilder, index: number, level: number) => booleana callback function to apply to each child nodeself?: booleanwhether to visit the current node along with child nodesrecursive?: booleanwhether to visit all descendant nodes in tree-order or only the immediate child nodesthisArg?: anyvalue to use as this when executing predicate
Returns boolean
filter
filter(predicate: (node: XMLBuilder, index: number, level: number) => boolean, self?: boolean, recursive?: boolean, thisArg?: any): XMLBuilder[]Produces an array of child nodes which pass the given predicate test.
predicate is called with three arguments: (node, index, level).
Parameters
predicate: (node: XMLBuilder, index: number, level: number) => booleana callback function to apply to each child nodeself?: booleanwhether to visit the current node along with child nodesrecursive?: booleanwhether to visit all descendant nodes in tree-order or only the immediate child nodesthisArg?: anyvalue to use as this when executing predicate
Returns XMLBuilder[]
find
find(predicate: (node: XMLBuilder, index: number, level: number) => boolean, self?: boolean, recursive?: boolean, thisArg?: any): XMLBuilder | undefinedReturns the first child node satisfying the given predicate. predicate is
called with three arguments: (node, index, level).
Parameters
predicate: (node: XMLBuilder, index: number, level: number) => booleana callback function to apply to each child nodeself?: booleanwhether to visit the current node along with child nodesrecursive?: booleanwhether to visit all descendant nodes in tree-order or only the immediate child nodesthisArg?: anyvalue to use as this when executing predicate
Returns XMLBuilder | undefined
first
first(): XMLBuilderReturns the first child node.
Returns XMLBuilder
import
import(node: XMLBuilder): XMLBuilderImports a node as a child node of this node. The nodes' descendants and attributes will also be imported.
Parameters
node: XMLBuilderthe node to import
Note: The node will be cloned before being imported and this clone will be inserted into the document; not the original node.
Note: If the imported node is a document, its document element node will be imported. If the imported node is a document fragment its child nodes will be imported.
Returns XMLBuilder
current element node
ins
ins(target: string, content?: string): XMLBuilderCreates a new processing instruction node and appends it to the list of child nodes.
Parameters
target: stringinstruction targetcontent?: stringnode content
Returns XMLBuilder
current element node
ins(target: PIObject): XMLBuilderCreates new processing instruction nodes by expanding the given object and appends them to the list of child nodes.
Parameters
-
target: PIObject
Returns XMLBuilder
current element node
last
last(): XMLBuilderReturns the last child node.
Returns XMLBuilder
map
map<T>(callback: (node: XMLBuilder, index: number, level: number) => T, self?: boolean, recursive?: boolean, thisArg?: any): T[]Produces an array of values by transforming each child node with the given
callback function. callback is called with three arguments:
(node, index, level).
Parameters
callback: (node: XMLBuilder, index: number, level: number) => Ta callback function to apply to each child nodeself?: booleanwhether to visit the current node along with child nodesrecursive?: booleanwhether to visit all descendant nodes in tree-order or only the immediate child nodesthisArg?: anyvalue to use as this when executing callback
Returns T[]
next
next(): XMLBuilderReturns the next sibling node.
Returns XMLBuilder
prev
prev(): XMLBuilderReturns the previous sibling node.
Returns XMLBuilder
reduce
reduce<T>(callback: (value: T, node: XMLBuilder, index: number, level: number) => T, initialValue: T, self?: boolean, recursive?: boolean, thisArg?: any): TReduces child nodes into a single value by applying the given callback
function. callback is called with four arguments:
(value, node, index, level).
Parameters
callback: (value: T, node: XMLBuilder, index: number, level: number) => Ta callback function to apply to each child nodeinitialValue: Tinitial value of the iterationself?: booleanwhether to visit the current node along with child nodesrecursive?: booleanwhether to visit all descendant nodes in tree-order or only the immediate child nodesthisArg?: anyvalue to use as this when executing callback
Returns T
remove
remove(): XMLBuilderRemoves this node from the XML document.
Returns XMLBuilder
parent element node
removeAtt
removeAtt(name: string): XMLBuilderRemoves an attribute.
Parameters
name: stringattribute name
Returns XMLBuilder
current element node
removeAtt(names: string[]): XMLBuilderRemoves a list of attributes.
Parameters
names: string[]an array with attribute names
Returns XMLBuilder
current element node
removeAtt(namespace: string | null, name: string): XMLBuilderRemoves an attribute.
Parameters
namespace: string | nullnamespace of the attribute to removename: stringattribute name
Returns XMLBuilder
current element node
removeAtt(namespace: string | null, names: string[]): XMLBuilderRemoves a list of attributes.
Parameters
namespace: string | nullnamespace of the attributes to removenames: string[]an array with attribute names
Returns XMLBuilder
current element node
root
root(): XMLBuilderReturns the root element node.
Returns XMLBuilder
set
set(builderOptions: Partial<XMLBuilderOptions>): XMLBuilderSets builder options.
Parameters
-
builderOptions: Partial<XMLBuilderOptions>
Returns XMLBuilder
current element node
some
some(predicate: (node: XMLBuilder, index: number, level: number) => boolean, self?: boolean, recursive?: boolean, thisArg?: any): booleanReturns true if any of the child nodes pass the given predicate test.
predicate is called with three arguments: (node, index, level).
Parameters
predicate: (node: XMLBuilder, index: number, level: number) => booleana callback function to apply to each child nodeself?: booleanwhether to visit the current node along with child nodesrecursive?: booleanwhether to visit all descendant nodes in tree-order or only the immediate child nodesthisArg?: anyvalue to use as this when executing predicate
Returns boolean
toArray
toArray(self?: boolean, recursive?: boolean): XMLBuilder[]Produces an array of child nodes.
Parameters
self?: booleanwhether to visit the current node along with child nodesrecursive?: booleanwhether to visit all descendant nodes in tree-order or only the immediate child nodes
Returns XMLBuilder[]
toObject
toObject(writerOptions?: ObjectWriterOptions): XMLSerializedAsObject | XMLSerializedAsObjectArrayConverts the node into its object representation.
Parameters
-
writerOptions?: ObjectWriterOptions
Returns XMLSerializedAsObject | XMLSerializedAsObjectArray
toObject(writerOptions: MapWriterOptions): XMLSerializedAsMap | XMLSerializedAsMapArrayConverts the node into its object representation using ES6 maps.
Parameters
-
writerOptions: MapWriterOptions
Returns XMLSerializedAsMap | XMLSerializedAsMapArray
toString
toString(writerOptions?: XMLWriterOptions): stringConverts the node into an XML string.
Parameters
-
writerOptions?: XMLWriterOptions
Returns string
toString(writerOptions: JSONWriterOptions): stringConverts the node into a JSON string.
Parameters
-
writerOptions: JSONWriterOptions
Returns string
toString(writerOptions: YAMLWriterOptions): stringConverts the node into a YAML string.
Parameters
-
writerOptions: YAMLWriterOptions
Returns string
txt
txt(content: string): XMLBuilderCreates a new text node and appends it to the list of child nodes.
Parameters
content: stringnode content
Returns XMLBuilder
current element node
up
up(): XMLBuilderReturns the parent node.
Returns XMLBuilder