libxml-to-js uses the string parser method of libxmljs. Basically a modified version of the algorithm from here in order to fit the formal specifications of xml2js output.
Either manually clone this repository into your node_modules directory, or the recommended method:
npm install libxml-to-js
The installation of the underlying dependency, libxmljs, fails if you don't have gcc (or compatible compiler), the libxml2 development headers, and the xml2-config script. Under various Linux distributions you may install the appropriate libxml2 development package: libxml2-dev (Debian, Ubuntu, etc), libxml2-devel (RHEL, CentOS, Fedora, etc).
var parser = require'libxml-to-js';var xml = 'xml string';parserxmlif errorconsole.errorerror;elseconsole.logresult;;
With XPath query:
parserxml '//xpath/query'if errorconsole.errorerror;elseconsole.logresult;;
Due to the fact that libxmljs does not have any method for returning the namespace attributes of a specific element, the returned namespaces aren't returned as expected:
- the returned namespaces are only the actual used namespaces by the XML document. If there are unused namespaces, they aren't returned. This is a consequence of the fact that the namespaces are pushed into the returned object as they are detected by the parsing recursion.
- the returned namespaces are attached as attributes to the root element, into the xmlns key in order to keep the code simple.
Example from the WordPress RSS 2 feed:
<!-- the rest of the doc -->
is parsed as:
'@':version: '2.0'xmlns:atom: ''sy: ''dc: ''content: ''wfw: ''slash: ''// the rest of the doc
- @Marsup: XPath queries support.
- @VirgileD: improved text kludge and namespaces support
- @Richard Anaya: fix for #6
- @TokyoIncidents: fixes a couple of global variables leaks #10
- @XApp-Studio: more support for CDATA elements #13