Skip to content

Commit

Permalink
return top-level node as single node unless there are multiples. also…
Browse files Browse the repository at this point in the history
… silenced some Titanium warnings (semicolons, bulletproofing)
  • Loading branch information
Todd Gehman committed Oct 8, 2010
1 parent e12213d commit 56bb797
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 34 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -3,7 +3,7 @@ PlistParser

PlistParser accepts a Property List expressed as an XML string and transforms it into a JavaScript data structure. Structure signifiers (ARRAY, DICT, KEY) in the XML are discarded - their data is returned as JavaScript arrays and associative arrays. Currently the only output option is JSON.

PlistParser does not rely on any external libraries like jQuery or JSON. It was written for use within the Appcelerator Titanium environment, but should also work with most modern browsers. Tested in Titanium, Firefox, IE, Safari, Chrome, and Opera.
PlistParser does not rely on any external libraries like jQuery or JSON. It was written for use within the Appcelerator Titanium environment, but also works with modern browsers. Tested in Titanium, Firefox, IE, Safari, Chrome, and Opera.

Usage
-----
Expand Down
62 changes: 29 additions & 33 deletions plist_parser.js
Expand Up @@ -4,6 +4,7 @@
Usage:
var jsonString = PlistParser.parse(xmlString);
*/

var PlistParser = {};
Expand All @@ -20,7 +21,7 @@ PlistParser.parse = function(plist_xml){

var result = this._xml_to_json(plist_xml.getElementsByTagName('plist').item(0));
return result;
}
};

PlistParser._xml_to_json = function(xml_node) {
var parser = this;
Expand All @@ -35,16 +36,15 @@ PlistParser._xml_to_json = function(xml_node) {
var child = parent_node.childNodes.item(i);
if (child.nodeName != '#text'){
child_nodes.push(child);
}
};
};

// console.log("working on " + parent_node_name)

switch(parent_node_name){

case 'plist':

if (child_nodes.length > 0){
if (child_nodes.length > 1){
// I'm not actually sure if it is legal to have multiple
// top-level nodes just below <plist>.
var plist_array = [];
for(var i = 0; i < child_nodes.length; ++i){
plist_array.push(parser._xml_to_json(child_nodes[i]));
Expand All @@ -54,10 +54,9 @@ PlistParser._xml_to_json = function(xml_node) {
// return plist_hash;
return plist_array;
} else {
return parser._xml_to_json(child_nodes.first)
return parser._xml_to_json(child_nodes[0]);
}
break;

case 'dict':

var dictionary = {};
Expand All @@ -68,15 +67,14 @@ PlistParser._xml_to_json = function(xml_node) {
if (child.nodeName == '#text'){
// ignore empty text children
} else if (child.nodeName == 'key'){
key_name = PlistParser._textValue(child.firstChild)
key_name = PlistParser._textValue(child.firstChild);
} else {
key_value = parser._xml_to_json(child);
dictionary[key_name] = key_value;
}
}

return dictionary;
break;

case 'array':

Expand All @@ -86,46 +84,45 @@ PlistParser._xml_to_json = function(xml_node) {
standard_array.push(parser._xml_to_json(child));
}
return standard_array;
break;

case 'string':

return PlistParser._textValue(parent_node);
break;

case 'date':

var date = PlistParser._parseDate(PlistParser._textValue(parent_node));
return date.toString();
break;

case 'integer':

return parseInt(PlistParser._textValue(parent_node));
break;
return parseInt(PlistParser._textValue(parent_node), 10);

case 'real':

return parseFloat(PlistParser._textValue(parent_node));
break;

case 'data':

return PlistParser._textValue(parent_node);

case 'true':

return true;
break;

case 'false':

return false;
break;


case '#text':

break;
}
}
};
};

// Lifted from: http://blog.stchur.com/2007/04/06/serializing-objects-in-javascript/
// Lifted (slightly modified) from:
// http://blog.stchur.com/2007/04/06/serializing-objects-in-javascript/
PlistParser.serialize = function(_obj) {
// Let Gecko browsers do this the easy way
if (typeof _obj.toSource !== 'undefined' && typeof _obj.callee === 'undefined')
Expand All @@ -143,12 +140,10 @@ PlistParser.serialize = function(_obj) {
case 'boolean':
case 'function':
return _obj;
break;

// for JSON format, strings need to be wrapped in quotes
case 'string':
return '\'' + _obj + '\'';
break;

case 'object':
var str;
Expand All @@ -163,26 +158,27 @@ PlistParser.serialize = function(_obj) {
{
str = '{';
var key;
for (key in _obj) { str += key + ':' + PlistParser.serialize(_obj[key]) + ','; }
for (key in _obj) {
// "The body of a for in should be wrapped in an if statement to filter unwanted properties from the prototype."
if (_obj.hasOwnProperty(key)) {
str += key + ':' + PlistParser.serialize(_obj[key]) + ','; }
}
str = str.replace(/\,$/, '') + '}';
}
return str;
break;

default:
return 'UNKNOWN';
break;
}

}
};
};

PlistParser._textValue = function(node) {
if (node.text){
return node.text;
} else {
return node.textContent;
}
}
};
};

// Handle date parsing in non-FF browsers
// Thanks to http://www.west-wind.com/weblog/posts/729630.aspx
Expand All @@ -191,6 +187,6 @@ PlistParser._parseDate = function(date_string){
var matched_date = reISO.exec(date_string);
if (matched_date){
return new Date(Date.UTC(+matched_date[1], +matched_date[2] - 1, +matched_date[3], +matched_date[4], +matched_date[5], +matched_date[6]));
}
}
};
};

0 comments on commit 56bb797

Please sign in to comment.