Clean wrapper around the
sax library to read, manipulate and render xml files like an artist.
Quick example :
<!-- input.xml -->
const XML =// we get the data from the given xml fileconst data = XML// we find all nodes which name begin with `hero:` and which have the attribute `name`// and we replace them by some textfor const node of datanode// we write the transformed xml to an output filedata
<!-- output.xml -->I'm a hero and my name is FooI'm a hero and my name is Bar
Read more to discover the awesome versatility and ease-of-use of the library.
There are several ways to create the data tree from xml data :
const XML =// with template stringsconst data = XML `<heroes><hero:ally name="Foo"/><hero:enemy name="Bar"/><hero:enemy/></heroes>`// with the parse functionconst data = XML
You can also get your
XmlNode tree from previously created JSON data :
const data = XMLconst json = dataconst sameDataButFromJson = XML
The default options are :
The XmlNode object
XmlNode has the field
children which can contain plain text or other
processingInstructions field is not always defined and contains the
body informations of tags which have the form :
The constructor takes as single argument a XmlNode-like object :
find(name: String|RegExp): return the first XmlNode with the matching name, or
nullif no result is found.
find(attributes: [String]): same, but take an array of attributes as argument. Exemple :
find(['id', 'class=foo*'])will find the first node which have the attribute
idand a value beginning by 'foo' for the attribute
find(name: String|RegExp, attributes: [String]): same, but with conditions on the name and the attributes.
find(callback: Function): You can also specifiy your own function to match the desired node. Your function must return the node itself it is valid, or a null value otherwise.
Every string passed to these functions will be transformed into regular expressions with this very simple rule : all
* characters will be treated as
Be aware that if you use special characters (like
]) they will be treated as part of the regular expression unless you double-escape them.
The following finder functions all take the same argument(s) :
findAll: return an array of nodes instead of the first one.
findChild: do not search recursively, only amongst direct children. Return the first result.
findAllChildren: Return all the result amongst direct children only.
findParent: Return the first matching parent.
findAllParents: Return an array of all matching parents (closer parents first).
You pass an object to the
walk function that can have up to five callback functions, one for each type of child a xmlNode can have.
If a non-null (ie not
null and not
undefined) value is returned by one of the callback functions, the walking stops and this value is returned by the
If you need to pass asynchronous functions as callbacks, call the asynchronous version of the walker :
asyncWalk, you can mix
Tree mutator methods
push(element: XmlNode|String, before?: Integer|XmlNode): push a new child. The
beforeparameter indicates before which XmlNode element (or index) the new child should be pushed.
push(arrayOfElements: [XmlNode|String], before?: Integer|XmlNode): you can also push an array of elements, which can be very handful in combination with the
findAllmethod. You should use this method if you plan to push many elements at once for speed efficiency.
replaceWith(element: XmlNode|String): replace an element by another one. Note : if the element already belonged to the tree, it is moved (not copied). If you want to create a copy, use the
replaceWith(arrayOfElements: [XmlNode|String]): you can also replace with an array of elements, which can be very handful in combination with the
clone(): create a copy of an element. All children will be cloned too.
remove(): self-remove from the tree.
removeChild(element: XmlNode|String): remove the given child element.
pushTo(element: XmlNode, before?: Integer|XmlNode): move itself as a new child to the given XmlNode.
empty(): remove all children.
toXml(pretty: Boolean): Generate XML, pretty-printed or not. If you want to pretty-print your xml, you should consider to set the
toXmlFile(filename: String, pretty: Boolean): Generate XML and write it to the given file.
toJson(): Create clean and reusable JSON.
Other methods (and getters)
get innerText(): Return the text of the node.
Be an artist