Skip to content
This repository was archived by the owner on Jan 7, 2021. It is now read-only.

Commit d80fb05

Browse files
committed
initial commit
0 parents  commit d80fb05

8 files changed

Lines changed: 164 additions & 0 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
*.swp

README.md

Whitespace-only changes.

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./lib');

lib/index.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
var expat = require('node-expat');
2+
var fs = require('fs');
3+
var parser = new expat.Parser('UTF-8');
4+
5+
// This object will hold the final result.
6+
var obj = {};
7+
var currentParent = obj;
8+
var ancestors = [];
9+
10+
function startElement(name, attrs) {
11+
if (! (name in currentParent)) {
12+
currentParent[name] = attrs;
13+
} else if (!(currentParent[name] instanceof Array)) {
14+
// Put the existing object in an array.
15+
var newArray = [currentParent[name]];
16+
// Add the new object to the array.
17+
newArray.push(attrs);
18+
// Point to the new array.
19+
currentParent[name] = newArray;
20+
} else {
21+
// An array already exists, push the attributes on to it.
22+
currentParent[name].push(attrs);
23+
}
24+
25+
// Store the current (old) parent.
26+
ancestors.push(currentParent);
27+
28+
// We are now working with this object, so it becomes the current parent.
29+
if (currentParent[name] instanceof Array) {
30+
// If it is an array, get the last element of the array.
31+
currentParent = currentParent[name][currentParent[name].length - 1];
32+
} else {
33+
// Otherwise, use the object itself.
34+
currentParent = currentParent[name];
35+
}
36+
}
37+
38+
function charData(data) {
39+
data = data.trim();
40+
if (!data.length) {
41+
return;
42+
}
43+
currentParent['$t'] = data;
44+
}
45+
46+
function endElement(name) {
47+
// This should check to make sure that the name we're ending
48+
// matches the name we started on.
49+
50+
var ancestor = ancestors.pop();
51+
if ((Object.keys(currentParent).length == 1) && ('$t' in currentParent)) {
52+
if (ancestor[name] instanceof Array) {
53+
//console.log("list-replacing $t in " + name);
54+
ancestor[name].push(ancestor[name].pop()['$t']);
55+
} else {
56+
//console.log("replacing $t in " + name);
57+
ancestor[name] = currentParent['$t'];
58+
}
59+
} else {
60+
//console.log("final " + name + ":");
61+
//console.log(currentParent);
62+
}
63+
64+
currentParent = ancestor;
65+
}
66+
67+
parser.on('startElement', startElement);
68+
parser.on('text', charData);
69+
parser.on('endElement', endElement);
70+
71+
module.exports.toJson = function(xml, _object) {
72+
_object = _object || false;
73+
if(parser.parse(xml)) {
74+
if(_object) {
75+
return obj;
76+
}
77+
return JSON.stringify(obj);
78+
}
79+
};
80+
81+

package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{ "name" : "xml2json",
2+
"version": "0.1.0",
3+
"author": "Andrew Turley",
4+
"email": "aturley@buglabs.net",
5+
"description" : "Converts xml to json using node-expat.",
6+
"repository": "git://github.com/buglabs/node-xml2json.git",
7+
"main": "index",
8+
"maintainers":[ {
9+
"name": "Camilo Aguilar",
10+
"email": "camilo@buglabs.net"}
11+
],
12+
"dependencies": {
13+
"node-expat": "1.3.2"
14+
}
15+
}
16+

test/fixtures/domain.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"domain":{"type":"qemu","name":"QEmu-fedora-i686","uuid":"c7a5fdbd-cdaf-9455-926a-d65c16db1809","memory":"219200","currentMemory":"219200","vcpu":"2","os":{"type":{"arch":"i686","machine":"pc","$t":"hvm"},"boot":{"dev":"cdrom"}},"devices":{"emulator":"/usr/bin/qemu-system-x86_64","disk":[{"type":"file","device":"cdrom","source":{"file":"/home/user/boot.iso"},"target":{"dev":"hdc"},"readonly":{}},{"type":"file","device":"disk","source":{"file":"/home/user/fedora.img"},"target":{"dev":"hda"}}],"interface":{"type":"network","source":{"network":"default"}},"graphics":{"type":"vnc","port":"-1"}},"ah":[{"type":"rare","foo":"bar","$t":"cosa1"},{"type":"normal","$t":"cosa2"},"cosa3"]}}

test/fixtures/domain.xml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<domain type='qemu'>
2+
<name>QEmu-fedora-i686</name>
3+
<uuid>c7a5fdbd-cdaf-9455-926a-d65c16db1809</uuid>
4+
<memory>219200</memory>
5+
<currentMemory>219200</currentMemory>
6+
<vcpu>2</vcpu>
7+
<os>
8+
<type arch='i686' machine='pc'>hvm</type>
9+
<boot dev='cdrom'/>
10+
</os>
11+
<devices>
12+
<emulator>/usr/bin/qemu-system-x86_64</emulator>
13+
<disk type='file' device='cdrom'>
14+
<source file='/home/user/boot.iso'/>
15+
<target dev='hdc'/>
16+
<readonly/>
17+
</disk>
18+
<disk type='file' device='disk'>
19+
<source file='/home/user/fedora.img'/>
20+
<target dev='hda'/>
21+
</disk>
22+
<interface type='network'>
23+
<source network='default'/>
24+
</interface>
25+
<graphics type='vnc' port='-1'/>
26+
</devices>
27+
<ah type='rare' foo='bar'>cosa1</ah>
28+
<ah type='normal'>cosa2</ah>
29+
<ah>cosa3</ah>
30+
</domain>

test/test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
var fs = require('fs');
2+
var path = require('path');
3+
var parser = require('../lib');
4+
var assert = require('assert');
5+
6+
var fixturesPath = 'fixtures';
7+
8+
fs.readdir(fixturesPath, function(err, files) {
9+
for(var i in files) {
10+
var file = files[i];
11+
var ext = path.extname(file);
12+
13+
if(ext == '.xml') {
14+
var basename = path.basename(file, '.xml');
15+
16+
var data = fs.readFileSync(fixturesPath + '/' + file);
17+
var result = parser.toJson(data);
18+
19+
var jsonFile = basename + '.json'
20+
var expected = fs.readFileSync(fixturesPath + '/' + jsonFile) + '';
21+
22+
if(result) {
23+
result = result.trim();
24+
}
25+
26+
if(expected) {
27+
expected = expected.trim();
28+
}
29+
30+
assert.deepEqual(result, expected, jsonFile + ' and ' + file + ' are different');
31+
}
32+
}
33+
});

0 commit comments

Comments
 (0)