libxml-to-js
Advanced tools
Comparing version 0.3.3 to 0.3.4
@@ -0,1 +1,4 @@ | ||
## v0.3.4 | ||
* XPath queries support for parsing just parts of the XML document. Thanks to @[Marsup](https://github.com/Marsup) for the contribution. | ||
## v0.3.3 | ||
@@ -2,0 +5,0 @@ * Refactored the namespace support in order to make it more stable. The parser used to crash for large XML documents in an undeterministic manner (missing method errors or segmentation faults, for the same input). |
@@ -32,10 +32,13 @@ var libxmljs = require('libxmljs'); | ||
var libxml2js = function (obj, recurse, namespaces) { | ||
if (namespaces == undefined) { | ||
namespaces = {}; | ||
} | ||
if ( ! recurse) { | ||
obj = obj.root(); | ||
if (obj.namespace()) { | ||
namespaces['xmlns'] = obj.namespace().href(); | ||
} | ||
} | ||
if (namespaces == undefined) { | ||
namespaces = {}; | ||
} | ||
var jsobj = {}, children = obj.childNodes(), attributes = obj.attrs(); | ||
@@ -130,5 +133,18 @@ | ||
module.exports = function (xml, callback) { | ||
module.exports = function (xml, xpath, callback) { | ||
if(!callback) { | ||
callback = xpath; | ||
xpath = null; | ||
} | ||
var xmlDocument, jsDocument, selected = []; | ||
try { | ||
callback(null, libxml2js(libxmljs.parseXmlString(xml))); | ||
xmlDocument = libxmljs.parseXmlString(xml); | ||
jsDocument = libxml2js(xmlDocument); | ||
if(!!xpath) { | ||
xmlDocument.find(xpath, jsDocument['@'].xmlns).forEach(function(item) { | ||
selected.push(libxml2js(item, true).jsobj); | ||
}); | ||
return callback(null, selected); | ||
} | ||
callback(null, jsDocument); | ||
} catch (err) { | ||
@@ -135,0 +151,0 @@ err.message = err.message || 'libxml error'; |
{ | ||
"name": "libxml-to-js", | ||
"version": "0.3.3", | ||
"version": "0.3.4", | ||
"main": "./lib/libxml-to-js.js", | ||
@@ -17,2 +17,10 @@ "description": "XML to JavaScript object parser based on libxmljs", | ||
}, | ||
"contributors": [ { | ||
"name": "Brian White", | ||
"url": "http://mscdex.net/" | ||
}, | ||
{ | ||
"name": "Marsup" | ||
} | ||
], | ||
"repository": { | ||
@@ -22,3 +30,4 @@ "type": "git", | ||
}, | ||
"keywords": ["xml", "javascript", "object", "parser", "libxml", "libxmljs", "namespace", "cdata"], | ||
"keywords": ["xml", "javascript", "object", "parser", "libxml", "libxmljs", | ||
"namespace", "cdata", "xpath"], | ||
"scripts": { | ||
@@ -25,0 +34,0 @@ "test": "make test" |
@@ -28,2 +28,14 @@ ## About | ||
With XPath query: | ||
```javascript | ||
parser(xml, '//xpath/query', function (error, result) { | ||
if (error) { | ||
console.error(error); | ||
} else { | ||
console.log(result); | ||
} | ||
}); | ||
``` | ||
## Gotcha | ||
@@ -67,1 +79,6 @@ | ||
``` | ||
## Contributors | ||
* @[Brian White](https://github.com/mscdex): The original algorithm for converting a parsed XML doc into JavaScript object. | ||
* @[Marsup](https://github.com/Marsup): XPath queries support. |
@@ -11,1 +11,7 @@ var parser = require('../'); | ||
}); | ||
parser(fs.readFileSync('data/ec2-describeimages.xml').toString(), '//xmlns:blockDeviceMapping', function (err, res) { | ||
assert.ifError(err); | ||
assert.strictEqual(res.length, 2); | ||
assert.strictEqual(res[0].item.deviceName, '/dev/sda'); | ||
}); |
@@ -15,1 +15,7 @@ var parser = require('../'); | ||
}); | ||
parser(fs.readFileSync('data/wordpress-rss2.xml').toString(), '//dc:creator', function (err, res) { | ||
assert.ifError(err); | ||
assert.strictEqual(res.length, 1); | ||
assert.strictEqual(res[0]['#'], 'admin'); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
63923
201
83