Comparing version 1.0.9 to 1.0.10
{ | ||
"name": "pixl-xml", | ||
"version": "1.0.9", | ||
"version": "1.0.10", | ||
"description": "A simple module for parsing and composing XML.", | ||
@@ -5,0 +5,0 @@ "author": "Joseph Huckaby <jhuckaby@gmail.com>", |
@@ -12,3 +12,3 @@ # Overview | ||
* Can convert all keys to lower-case | ||
* Can serialize objects back to pretty-printed XML | ||
* Can serialize objects back to pretty-printed or compact XML | ||
@@ -196,2 +196,23 @@ # Usage | ||
### Compact XML | ||
To produce compact XML output (i.e. without indentation nor EOLs) you simply have to call `stringify()` with three additional arguments. Pass in `0` indicating a zero indent level, and then two empty strings, one representing the indentation character (defaults to `\t`) and finally the EOL character (defaults to `\n`). Example: | ||
```javascript | ||
var xml_string = XML.stringify( doc, 'Document', 0, "", "" ); | ||
console.log( xml_string ); | ||
``` | ||
This would produce something like: | ||
```xml | ||
<?xml version="1.0"?><Document><Node Key="Value">Content</Node><Simple>Hello</Simple></Document> | ||
``` | ||
If you are composing an XML document which has the document root node preserved (see [preserveDocumentNode](#preserveDocumentNode) above), simply pass in an empty string for the name parameter. Example: | ||
```js | ||
var xml_string = XML.stringify( doc, "", 0, "", "" ); | ||
``` | ||
# Object-Oriented API | ||
@@ -246,2 +267,18 @@ | ||
## Custom XML Formatting | ||
To produce custom XML formatting using the object-oriented API, you can pass two arguments to the `compose()` method. The first argument represents the indentation character (defaults to `\t`) and the second argument represents the EOL character (defaults to `\n`). | ||
For example, to produce compact XML (i.e. all crammed onto one line) set both arguments to empty strings. Example: | ||
```js | ||
console.log( parser.compose("", "") ); | ||
``` | ||
This would produce the following output: | ||
```xml | ||
<?xml version="1.0" encoding="UTF-8"?><Document><Node Key="Value">Complex</Node><Simple>Hello, I changed this.</Simple></Document> | ||
``` | ||
# Utility Functions | ||
@@ -248,0 +285,0 @@ |
48
xml.js
@@ -32,3 +32,2 @@ /* | ||
var indent_string = "\t"; | ||
var xml_header = '<?xml version="1.0"?>'; | ||
@@ -392,9 +391,10 @@ var sort_args = null; | ||
XML.prototype.compose = function() { | ||
XML.prototype.compose = function(indent_string, eol) { | ||
// compose tree back into XML | ||
if (typeof(eol) == 'undefined') eol = "\n"; | ||
var tree = this.tree; | ||
if (this.preserveDocumentNode) tree = tree[this.documentNodeName]; | ||
var raw = compose_xml( tree, this.documentNodeName ); | ||
var body = raw.substring( raw.indexOf("\n") + 1, raw.length ); | ||
var raw = compose_xml( tree, this.documentNodeName, 0, indent_string, eol ); | ||
var body = raw.replace(/^\s*\<\?.+?\?\>\s*/, ''); | ||
var xml = ''; | ||
@@ -404,7 +404,7 @@ | ||
for (var idx = 0, len = this.piNodeList.length; idx < len; idx++) { | ||
xml += '<' + this.piNodeList[idx] + '>' + "\n"; | ||
xml += '<' + this.piNodeList[idx] + '>' + eol; | ||
} | ||
} | ||
else { | ||
xml += xml_header + "\n"; | ||
xml += xml_header + eol; | ||
} | ||
@@ -414,3 +414,3 @@ | ||
for (var idx = 0, len = this.dtdNodeList.length; idx < len; idx++) { | ||
xml += '<' + this.dtdNodeList[idx] + '>' + "\n"; | ||
xml += '<' + this.dtdNodeList[idx] + '>' + eol; | ||
} | ||
@@ -490,5 +490,7 @@ } | ||
var compose_xml = exports.stringify = function compose_xml(node, name, indent) { | ||
var compose_xml = exports.stringify = function compose_xml(node, name, indent, indent_string, eol) { | ||
// Compose node into XML including attributes | ||
// Recurse for child nodes | ||
if (typeof(indent_string) == 'undefined') indent_string = "\t"; | ||
if (typeof(eol) == 'undefined') eol = "\n"; | ||
var xml = ""; | ||
@@ -500,3 +502,3 @@ | ||
indent = 0; | ||
xml = xml_header + "\n"; | ||
xml = xml_header + eol; | ||
@@ -513,3 +515,3 @@ if (!name) { | ||
for (var k = 0; k < indent; k++) indent_text += indent_string; | ||
if ((typeof(node) == 'object') && (node != null)) { | ||
@@ -520,7 +522,7 @@ // node is object -- now see if it is an array or hash | ||
xml += indent_text + "<" + name; | ||
var num_keys = 0; | ||
var has_attribs = 0; | ||
for (var key in node) num_keys++; // there must be a better way... | ||
if (node["_Attribs"]) { | ||
@@ -534,13 +536,13 @@ has_attribs = 1; | ||
} // has attribs | ||
if (num_keys > has_attribs) { | ||
// has child elements | ||
xml += ">"; | ||
if (node["_Data"]) { | ||
// simple text child node | ||
xml += encode_entities(node["_Data"]) + "</" + name + ">\n"; | ||
xml += encode_entities(node["_Data"]) + "</" + name + ">" + eol; | ||
} // just text | ||
else { | ||
xml += "\n"; | ||
xml += eol; | ||
@@ -552,7 +554,7 @@ var sorted_keys = hash_keys_to_array(node).sort(); | ||
// recurse for node, with incremented indent value | ||
xml += compose_xml( node[key], key, indent + 1 ); | ||
xml += compose_xml( node[key], key, indent + 1, indent_string, eol ); | ||
} // not _Attribs key | ||
} // foreach key | ||
xml += indent_text + "</" + name + ">\n"; | ||
xml += indent_text + "</" + name + ">" + eol; | ||
} // real children | ||
@@ -562,3 +564,3 @@ } | ||
// no child elements, so self-close | ||
xml += "/>\n"; | ||
xml += "/>" + eol; | ||
} | ||
@@ -570,3 +572,3 @@ } // standard node | ||
// recurse for node in array with same indent | ||
xml += compose_xml( node[idx], name, indent ); | ||
xml += compose_xml( node[idx], name, indent, indent_string, eol ); | ||
} | ||
@@ -577,5 +579,5 @@ } // array of nodes | ||
// node is simple string | ||
xml += indent_text + "<" + name + ">" + encode_entities(node) + "</" + name + ">\n"; | ||
xml += indent_text + "<" + name + ">" + encode_entities(node) + "</" + name + ">" + eol; | ||
} // simple text node | ||
return xml; | ||
@@ -582,0 +584,0 @@ }; |
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
31962
521
437