- Element: any XML Element
- Text nodes are Strings
- Runs on node.js and browserify
Parse a little document at once:
el = ltx.parse("<document/>")
Push parser:
p = new ltx.Parser();
p.on('tree', function(tree) {
proceed(null, tree);
});
p.on('error', function(error) {
proceed(error);
});
ltx implements multiple SAX backends:
- node-expat: libexpat binding
- ltx: fast native-JavaScript parser without error handling
- saxjs: native-JavaScript parser
If present, they are available through
ltx.availableSaxParsers
. Mostly, you'll want to do:
parser = new ltx.bestSaxParser();
Refer to lib/parse.js
for the interface.
is(name, xmlns?)
: checkgetName()
: name without ns prefixgetNS()
: element's xmlns, respects prefixes and searches upwardsfindNS(prefix?)
: search for xmlns of a prefix upwardsgetChild(name, xmlns?)
: find first childgetChildren(name, xmlns?)
: find all childrengetChildByAttr(attr, value, xmlns?, recursive?)
: find first child by a specific attributegetChildrenByAttr(attr, value, xmlns?, recursive?)
: find all children by a specific attributegetText()
: appends all text nodes recursivelygetChildText(name)
: a child's text contentsroot()
: uppermost parent in the treeup()
: parent or self
attrs
is an object of the Element's attributesname
contains optional prefix, colon, nameparent
points to its parent, this should always be consistent with childrenchildren
is an Array of Strings and Elements
new Element(name, attrs?)
: constructorremove(child)
: remove child by referenceremove(name, xmlns)
: remove child by tag name and xmlnsattr(attrName, value?)
: modify or get an attribute's valuetext(value?)
: modify or get the inner textclone()
: clones an element that is detached from the document
el = new ltx.Element('root').
c('children');
el.c('child', { age: 5 }).t('Hello').up()
.c('child', { age: 7 }).t('Hello').up()
.c('child', { age: 99 }).t('Hello').up()
console.log("Serialized document:", el.root().toString());
This resembles Strophejs a bit.
strophejs' XML Builder is very convenient for producing XMPP
stanzas. node-xmpp includes it in a much more primitive way: the
c()
, cnode()
and t()
methods can be called on any Element
object, returning the child element.
This can be confusing: in the end, you will hold the last-added child
until you use up()
, a getter for the parent. Connection.send()
first invokes tree()
to retrieve the uppermost parent, the XMPP
stanza, before sending it out the wire.
Please always make sure parent
and children
are consistent. Don't
append children of other parents to your own element. We're not
adoption-safe!
- More documentation
- More tests (Using Vows)