Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

libxml-to-js

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

libxml-to-js

XML to JavaScript object parser based on libxmljs

  • 0.3.9
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
374
increased by5.95%
Maintainers
1
Weekly downloads
 
Created
Source

About

This is a XML to JavaScript object parser. It uses the libxmljs module for the actual XML parsing. It aims to be an easy xml2js replacement, but it doesn't follow the xml2js API. I used xml2js for my own needs, but the error reporting of the underlying SAX parser is quite broken. This is how libxml-to-js saw the day light.

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.

Installation

Either manually clone this repository into your node_modules directory, or the recommended method:

npm install libxml-to-js

Usage mode

var parser = require('libxml-to-js');
var xml = 'xml string';

parser(xml, function (error, result) {
	if (error) {
		console.error(error);
	} else {
		console.log(result);
	}
});

With XPath query:

parser(xml, '//xpath/query', function (error, result) {
	if (error) {
		console.error(error);
	} else {
		console.log(result);
	}
});

Gotcha

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 into an obvious way:

  • 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:

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>
<!-- the rest of the doc -->
</rss>

is parsed as:

{ '@': 
   { version: '2.0',
     xmlns: 
      { atom: 'http://www.w3.org/2005/Atom',
        sy: 'http://purl.org/rss/1.0/modules/syndication/',
        dc: 'http://purl.org/dc/elements/1.1/',
        content: 'http://purl.org/rss/1.0/modules/content/',
        wfw: 'http://wellformedweb.org/CommentAPI/',
        slash: 'http://purl.org/rss/1.0/modules/slash/' } },
// the rest of the doc
}

Contributors

  • @Brian White: The original algorithm for converting a parsed XML doc into JavaScript object.
  • @Marsup: XPath queries support.
  • @VirgileD: improved text kludge and namespaces support
  • @Richard Anaya: fix for #6

Keywords

FAQs

Package last updated on 05 Dec 2011

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc