For more on why I wrote this class, see the blog post.
See CHANGELOG.md for details.
npm install xmldoc
Or just download the repository and include it in your
node_modules directly. Or just download the single JS file!
var xmldoc = ;var document = "<some>xml</some>";// do things
The primary exported class is
XmlDocument, which you'll use to consume your XML text.
XmlDocument contains a hierarchy of
XmlElement instances representing the XML structure.
XmlDocument contain the same members and methods you can call to traverse the document or a subtree.
name- the node name, like "tat" for
<tat>. XML "namespaces" are ignored by the underlying sax-js parser, so you'll simply get "office:body" for
attr- an object dict containing attribute properties, like
val- the string "value" of the node, if any, like "world" for
children- an array of
XmlElementchildren of the node.
lastChild- pretty much what it sounds like; null if no children
startTagPosition- information about the element's original position in the XML string.
Each member defaults to a sensible "empty" value like
All methods with
child in the name operate only on direct children; they do not do a deep/recursive search.
It's important to note that
xmldoc is designed for when you know exactly what you want from your XML file. For instance, it's great for parsing API responses with known structures, but it's not great at teasing things out of HTML documents from the web.
If you need to do lots of searching through your XML document, I highly recommend trying a different library like node-elementtree.
Similar to underscore's
each method, it will call
func(child, index, array) for each child of the given node.
Pass it the name of a child node and it will search for and return the first one found, or
childNamed but returns all matching children in an array, or
Searches for the first child with the given attribute value. You can omit
value to just find the first node with the given attribute defined at all.
Searches for a specific "path" using dot notation. Example:
George R. R. Martin......
If you just want the
<name> node and you have the
XmlElement for the
<book> node, you can say:
var nameNode = bookNode; // return <name> node
descendantWithPath, but goes deeper and extracts the
val of the node. Example:
var authorName = bookNode; // return "George R. R. Martin"
You can also use the
@ character to request the value of a particular attribute instead:
var authorIsProper = bookNode; // return "true"
This is not XPath! It's just a thing I made up, OK?
The default implementation of
toString(), that is, the one you get when you just
console.log("Doc: " + myDoc) will pretty-print the XML with linebreaks and indents. You can pass a couple options to control the output:
xml // strips indents and linebreaksxml // trims long strings for easier debuggingxml // prevents whitespace being removed from around element values
Putting it all together:
var xml = "<author><name>looooooong value</name></author>";console
My Document: <hello> loooooooo… </hello>
Feel free to file issues or hit me up on Twitter.