Trivial XML
Simple parsing and building of XML documents, powered by new features only available in ES6.
The goals of this project:
- To make XML parsing require little-to-no error checking. Exploring the node tree should be as forgiving as possible.
- To provide an easy way to open, modify and save an XML document in a single, unified library.
- To be easily installed without any native code compilation.
- To provide a novel and terse API by taking advantage of the latest ES6 metaprogramming features.
- Useful for quick and dirty jobs which don't need fine grained control over every intricacy of XML.
- Prioritising ease-of-use over speed.
- To make you re-think your approach to XML parsing.
Requires a recent version of Node which has Proxy support.
Examples
Parsing an XML document
var trixml = ;var xml = trixml; xml // {an: "attribute", another: "one"}xmlname; // "Root"xmlvalue; // ""xmlEnvelopeMessagevalue; // "Hello"xmlEnvelopeMessage // "Hello World !"xmlEnvelopeMessage // msg1xmlEnvelopeMessage // "msg1,msg2,msg3"
Building an XML document
var trixml = ;var xml = trixml; xml;xmlBody;xmlBody; // overwrites value & merges attributesxmlFooter;xmlFooter;xmlFooter;xmlFooter;xmlFooter;xmlTrailer; console; // <?xml version="1.0"?>// <root>// <Header>Hello ...</Header>// <Body>// <Message length="6" planet="Earth">World!</Message>// </Body>// <Footer>// <wibble a="b" c="d" />// <wobble>Easy!</wobble>// <wubble e="f">Simple</wubble>// <wubble />// <flub>fluub</flub>// </Footer>// <Trailer />// </root>
A basic RSS parser
var trixml = ;var https = ; { var xmlString = ""; https;} ;
Gotchas
It's possible for nodes in your XML document to have names which conflict with the names of members of the XMLNode
or XMLNodeCollection
objects. When this happens, you cannot use the shorthand method for accessing child nodes.
The following XML nodes are affected:
<inspect>
, <toJSON>
, <toString>
, <value>
, <children>
, <comments>
, <attr>
, <addChild>
, <get>
, <cloneSync>
, <remove>
, <empty>
In these cases, you must use the longhand method like this:
var root = trixml; rootvalue === "gotcha"; // true // don't do this... (it won't work)rootinspectaddChildvalue; // TypeError: Cannot read property 'addChild' of null
Todo:
- Add support for streams
- Async parsing, returning Promises
- Ability to remove attributes
- Add support for more Array methods on XMLNodeCollection
- More tests