Skip to content
This repository has been archived by the owner on Jul 15, 2018. It is now read-only.

Commit

Permalink
changed e4x to xml throughout
Browse files Browse the repository at this point in the history
  • Loading branch information
olegp committed Oct 26, 2010
1 parent 8a9a428 commit 0a655d2
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 21 deletions.
16 changes: 6 additions & 10 deletions README.md
@@ -1,24 +1,20 @@
# ringo-easyxml

One way converter from E4X XML to JSON. Usage:
One way converter from XML to JSON. Usage:

var {E4XtoJSON} = require("easyxml");
var {XMLtoJSON} = require("easyxml");
var xml = '<a><b>c</b></a>';
var json = E4XtoJSON(xml);
var json = XMLtoJSON(xml);

Turns child nodes with the same name into arrays so that lists are easier
to work with.

<body><item>1</item><item>2</item></body>

body: {item: ["1", "2"]}
`<body><item>1</item><item>2</item></body>` becomes `body: {item: ["1", "2"]}`

Turns XML node attributes into object attributes,
the children are placed inside a special attribute named "_":

<body a="a">whatever</body>

body: {_a: "a", _: "whatever"}
`<body a="a">whatever</body>` becomes `body: {_a: "a", _: "whatever"}`

For other options, take a look at test/all.js.
For options, take a look at testOptions in `test/all.js.`

4 changes: 2 additions & 2 deletions lib/easyxml.js
@@ -1,4 +1,4 @@
export("E4XtoJSON");
export("XMLtoJSON");

function toXML(string) {
if(!(string instanceof XML)) {
Expand Down Expand Up @@ -67,6 +67,6 @@ function toJSON(xml, options) {
return r;
}

function E4XtoJSON(xml, options) {
function XMLtoJSON(xml, options) {
return toJSON(toXML(xml), setOptions(options));
}
2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"name": "ringo-easyxml",
"version": "0.1.0",
"description": "XML (E4X) to JSON converter",
"description": "XML to JSON converter",
"keywords": ["xml", "json"],
"author": "Oleg Podsechin (http://github.com/olegp)"
}
16 changes: 8 additions & 8 deletions test/all.js
@@ -1,43 +1,43 @@
include('assert');

var {E4XtoJSON} = require("easyxml");
var {XMLtoJSON} = require("easyxml");
var {get} = require("ringo/httpclient");

exports.setUp = function () {
}

exports.testBasic = function() {
var xml = '<a><b>c</b></a>';
var json = E4XtoJSON(xml);
var json = XMLtoJSON(xml);
equal(json.b, "c");
}

exports.testAttribute = function() {
var xml = '<a><b c="d"/></a>';
var json = E4XtoJSON(xml);
var json = XMLtoJSON(xml);
equal(json.b._c, "d");
}

exports.testOptions = function() {
var xml = '<a><b c="d">e</b></a>';

var json1 = E4XtoJSON(xml);
var json1 = XMLtoJSON(xml);
equal(json1.b._c, "d");
equal(json1.b._, "e");

var json2 = E4XtoJSON(xml, {simple: ["b"]});
var json2 = XMLtoJSON(xml, {simple: ["b"]});
equal(json2.b, "e");

var json3 = E4XtoJSON(xml, {ignored: ["c"]});
var json3 = XMLtoJSON(xml, {ignored: ["c"]});
equal(json3.b, "e");

var json4 = E4XtoJSON(xml, {prefix: ""});
var json4 = XMLtoJSON(xml, {prefix: ""});
equal(json4.b.c, "d");
}

exports.testCDATA = function() {
var xml = '<a><b><![CDATA[<c>d</c>]]></b></a>';
var json = E4XtoJSON(xml);
var json = XMLtoJSON(xml);
equal(json.b, "<c>d</c>");
}

Expand Down

0 comments on commit 0a655d2

Please sign in to comment.