├── .gitignore
├── app.js
├── LICENSE
├── README.md
└── XMLTools.js
/.gitignore:
--------------------------------------------------------------------------------
1 | Resources
2 | build
3 | tiapp.xml
4 |
--------------------------------------------------------------------------------
/app.js:
--------------------------------------------------------------------------------
1 | /*globals require*/
2 |
3 | var XMLTools = require("XMLTools");
4 | var parser = new XMLTools('RufuslabradorMartywhippet');
5 | Ti.API.info(parser.toJSON());
6 |
7 | /* will output:
8 | [INFO] {"dog":[{"name":{"text":"Rufus","type":"first"},"breed":"labrador"},{"name":"Marty","breed":"whippet"}],"cat":{"name":"Matilda"}}
9 | */
10 | var parser = new XMLTools('SnoopylabradorMartywhippet');
11 | Ti.API.info(parser.toJSON());
12 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (C) 2011 by YY Digital Pty Ltd
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining a copy
4 | of this software and associated documentation files (the "Software"), to deal
5 | in the Software without restriction, including without limitation the rights
6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 | copies of the Software, and to permit persons to whom the Software is
8 | furnished to do so, subject to the following conditions:
9 |
10 | The above copyright notice and this permission notice shall be included in
11 | all copies or substantial portions of the Software.
12 |
13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 | THE SOFTWARE.
20 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | XMLTools Javascript Module for Titanium
2 | =======================================
3 |
4 | Written in the style of http://www.thomasfrank.se/xml_to_json.html but
5 | for Titanium with extras.
6 | One new addition is the handling of nodes with both attributes and text
7 | content.
8 |
9 | See the `app.js` file for a test case/example.
10 |
11 | Quick Start
12 | -----------
13 | It is a commonJS module so:
14 |
15 | ~~~javascript
16 | var XMLTools = require("XMLTools");
17 | ~~~
18 |
19 | Code snippets
20 | -------------
21 |
22 | Create and parse xml string:
23 |
24 | ~~~javascript
25 | var xml = new XMLTools(your_xml_string);
26 | ~~~
27 |
28 | If you already have `Ti.XML.Document` object, it can be passed to the
29 | constructor instead.
30 |
31 | To get the xml **documentElement**:
32 |
33 | ~~~javascript
34 | var documentElement = xml.getDocument();
35 | ~~~
36 |
37 | To get the xml converted to an **object**:
38 |
39 | ~~~javascript
40 | var my_object = xml.toObject();
41 | ~~~
42 |
43 | To get the xml converted into **JSON**:
44 |
45 | ~~~javascript
46 | var my_json = xml.toJSON();
47 | ~~~
48 |
49 | If you just want to convert xml to json in one line:
50 |
51 | ~~~javascript
52 | var my_json = new XMLTools(your_xml_string).toJSON()
53 | ~~~
54 |
55 |
56 | Contributions
57 | -------------
58 | I'm happy for this module to grow/improve.
59 |
--------------------------------------------------------------------------------
/XMLTools.js:
--------------------------------------------------------------------------------
1 | /**
2 | * XMLTools: Titanium module to convert XML to objects
3 | * Copyright: 2013 David Bankier (http://www.yydigital.com)
4 | * License: http://opensource.org/licenses/MIT
5 | * Source: https://github.com/dbankier/XMLTools-For-Appcelerator-Titanium
6 | */
7 |
8 | // In the style of http://www.thomasfrank.se/xml_to_json.html but for Appcelerator with extras.
9 |
10 |
11 | var XMLTools = function(inputXml) {
12 | if(typeof inputXml == 'string'){
13 | this.doc = Ti.XML.parseString(inputXml).documentElement;
14 | }
15 | if(typeof inputXml == 'object'){
16 | this.doc = inputXml.documentElement;
17 | }
18 | };
19 |
20 | XMLTools.prototype.getDocument = function() {
21 | return this.doc;
22 | };
23 | var addToObject = function(obj, key, value) {
24 | if(obj[key] == null) {
25 | obj[key] = value;
26 | } else if(!(obj[key] instanceof Array)) {
27 | var tmp = obj[key];
28 | var arr = [tmp, value];
29 | obj[key] = arr;
30 | } else {
31 | obj[key].push(value);
32 | }
33 | return obj;
34 | };
35 | var traverseTree = function(node) {
36 | var textOnly = true;
37 | var part = {};
38 | if(node.hasChildNodes()) {
39 | for(var ch_index = 0; ch_index < node.childNodes.length; ch_index++) {
40 | var ch = node.childNodes.item(ch_index);
41 | if(ch.nodeName=='#text' && ch.textContent.replace(/\n/g,'').replace(/ /g,'') == "") continue;//skip blank text element
42 | if(ch.nodeType === 3 || ch.nodeType === ch.CDATA_SECTION_NODE) {//Text Node
43 | if (node.childNodes.length === 1 && !node.hasAttributes()) {
44 | return ch.textContent;
45 | } else {
46 | part.text = ch.textContent;
47 | }
48 | } else {
49 | part = addToObject(part, ch.tagName, traverseTree(ch));
50 | }
51 | }
52 | textOnly = false;
53 | }
54 | if(node.hasAttributes()) {
55 | for(var att_index = 0; att_index < node.attributes.length; att_index++) {
56 | var att = node.attributes.item(att_index);
57 | //part = addToObject(part, att.nodeName, att.nodeValue);
58 | part[att.nodeName] = att.nodeValue;
59 | }
60 | textOnly = false;
61 | }
62 | return part;
63 | };
64 | XMLTools.prototype.toObject = function() {
65 | if(this.doc == null){
66 | return null;
67 | }
68 | this.obj = traverseTree(this.doc);
69 | return this.obj;
70 | };
71 |
72 | XMLTools.prototype.toJSON = function() {
73 | if(this.doc == null){
74 | return null;
75 | }
76 | if(this.obj == null) {
77 | this.obj = traverseTree(this.doc);
78 | }
79 | return (JSON.stringify(this.obj));
80 | };
81 |
82 | module.exports = XMLTools;
83 |
--------------------------------------------------------------------------------