https://github.com/peerlibrary/node-xml4js
XML to JavaScript parser using XML Schema to guide conversion
https://github.com/peerlibrary/node-xml4js
Last synced: about 1 year ago
JSON representation
XML to JavaScript parser using XML Schema to guide conversion
- Host: GitHub
- URL: https://github.com/peerlibrary/node-xml4js
- Owner: peerlibrary
- License: bsd-3-clause
- Created: 2014-06-19T19:55:59.000Z (almost 12 years ago)
- Default Branch: master
- Last Pushed: 2016-05-24T15:51:32.000Z (about 10 years ago)
- Last Synced: 2025-04-14T12:54:43.820Z (about 1 year ago)
- Language: JavaScript
- Size: 2.45 MB
- Stars: 60
- Watchers: 10
- Forks: 11
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
node-xml4js
===========
`node-xml4js` is a [Node.js](nodejs.org) package providing an XML to JavaScript parser using XML Schema to guide
conversion. Main motivation is that instead of guessing the structure of an XML document from the document itself,
structure is created automatically from the XML Schema which makes output consistent for all XML documents, not just
one at hand. For example, arrays are always arrays even when there is only one element in the particular XML document,
and if there is only one element allowed, then there will be no one-element array wrapped around. This makes
programmatically traversing the structure much easier, because structure is consistent and predictable.
Package builds upon [node-xml2js](https://github.com/Leonidas-from-XIV/node-xml2js), detects and parses XML Schema
which is then used to transform JavaScript object into a consistent schema-driven structure. API follows that of
`node-xml2js`. By default it maps attributes to `$` field and values to `_` field. Values are converted from strings
to corresponding reasonable JavaScript type.
**Warning**: This package uses XML Schema only as much as needed to improve conversion of XML but it is not a full
XML validator and it will successfuly convert some otherwise invalid XML.
Examples
--------
XML taken from [XML Primer](http://www.w3.org/TR/xmlschema-0/#po.xml):
```xml
Alice Smith
123 Maple Street
Mill Valley
CA
90952
Robert Smith
8 Oak Avenue
Old Town
PA
95819
Hurry, my lawn is going wild!
Lawnmower
1
148.95
Confirm this is electric
Baby Monitor
1
39.98
1999-05-21
```
Without using a XML Schema to guide a conversion process, with explicit arrays turned on, you would get:
```json
{
"purchaseOrder": {
"$": {
"orderDate": "1999-10-20",
"xmlns": "http://www.example.com/PO"
},
"shipTo": [
{
"$": {
"country": "US"
},
"name": [
"Alice Smith"
],
"street": [
"123 Maple Street"
],
"city": [
"Mill Valley"
],
"state": [
"CA"
],
"zip": [
"90952"
]
}
],
"billTo": [
{
"$": {
"country": "US"
},
"name": [
"Robert Smith"
],
"street": [
"8 Oak Avenue"
],
"city": [
"Old Town"
],
"state": [
"PA"
],
"zip": [
"95819"
]
}
],
"comment": [
"Hurry, my lawn is going wild!"
],
"items": [
{
"item": [
{
"$": {
"partNum": "872-AA"
},
"productName": [
"Lawnmower"
],
"quantity": [
"1"
],
"USPrice": [
"148.95"
],
"comment": [
"Confirm this is electric"
]
},
{
"$": {
"partNum": "926-AA"
},
"productName": [
"Baby Monitor"
],
"quantity": [
"1"
],
"USPrice": [
"39.98"
],
"shipDate": [
"1999-05-21"
]
}
]
}
]
}
}
```
But this package gives you this:
```json
{
"purchaseOrder": {
"$": {
"orderDate": "1999-10-20T00:00:00.000Z"
},
"shipTo": {
"$": {
"country": "US"
},
"name": "Alice Smith",
"street": "123 Maple Street",
"city": "Mill Valley",
"state": "CA",
"zip": 90952
},
"billTo": {
"$": {
"country": "US"
},
"name": "Robert Smith",
"street": "8 Oak Avenue",
"city": "Old Town",
"state": "PA",
"zip": 95819
},
"comment": "Hurry, my lawn is going wild!",
"items": {
"item": [
{
"$": {
"partNum": "872-AA"
},
"productName": "Lawnmower",
"quantity": 1,
"USPrice": 148.95,
"comment": "Confirm this is electric"
},
{
"$": {
"partNum": "926-AA"
},
"productName": "Baby Monitor",
"quantity": 1,
"USPrice": 39.98,
"shipDate": "1999-05-21T00:00:00.000Z"
}
]
}
}
}
```
Installation
------------
You can use [npm](https://npmjs.org/) to install it with:
```
npm install xml4js
```
Usage
-----
```javascript
var util = require('util');
var xml4js = require('xml4js');
// Will automatically download and use any missing schemas
xml4js.parseString(xml, {downloadSchemas: true}, function (err, result) {
console.log(util.inspect(result, false, null));
});
```
```javascript
var fs = require('fs');
var util = require('util');
var xml4js = require('xml4js');
// Most of xml2js options should still work
var options = {};
var parser = new xml4js.Parser(options);
// Default is to not download schemas automatically, so we should add it manually
var schema = fs.readFileSync('schema.xsd', {encoding: 'utf-8'});
parser.addSchema('http://www.example.com/Schema', schema, function (err, importsAndIncludes) {
// importsAndIncludes contains schemas to be added as well to satisfy all imports and includes found in schema.xsd
parser.parseString(xml, function (err, result) {
console.log(util.inspect(result, false, null));
});
});
```