-
Notifications
You must be signed in to change notification settings - Fork 1
/
XmlToJson.hx
162 lines (139 loc) · 3.62 KB
/
XmlToJson.hx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import haxe.Json;
#if neko
import sys.FileSystem;
import sys.io.File;
import Sys.println;
#end
using StringTools;
class XmlToJson
{
#if neko
static function main()
{
var cwd = Sys.getCwd();
var xmlFile = cwd + "haxelib.xml";
var jsonFile = cwd + "haxelib.json";
if (!FileSystem.exists(xmlFile))
{
println('File haxelib.xml was not found in the current directory.');
Sys.exit(0);
}
var xmlString = File.getContent(xmlFile);
var json = convert(xmlString);
var jsonString = prettyPrint(json);
println("START");
println("-----");
println(jsonString);
println("-----");
println("END");
File.saveContent(jsonFile, jsonString);
println('Saved to $jsonFile');
}
#end
#if js
static inline function jQ(input:Dynamic) return new js.JQuery(input);
static function main()
{
jQ(function () {
var xmlTA = jQ("#xml");
var jsonTA = jQ("#json");
var convertBtn = jQ("#convert");
xmlTA.text(sampleXml);
convertBtn.click(function (e) {
var json = convert(xmlTA.val());
jsonTA.text(prettyPrint(json));
});
jQ("textarea").focus(function (e) {
js.JQuery.cur.select();
return false;
});
});
}
#end
static function convert(inXml:String)
{
// Set up the default JSON structure
var json = {
"name": "",
"url" : "",
"license": "",
"tags": [],
"description": "",
"version": "0.0.1",
"releasenote": "",
"contributors": [],
"dependencies": {}
};
// Parse the XML and set the JSON
var xml = Xml.parse(inXml);
var project = xml.firstChild();
json.name = project.get("name");
json.license = project.get("license");
json.url = project.get("url");
for (node in project)
{
switch (node.nodeType)
{
case Xml.Element:
switch (node.nodeName)
{
case "tag":
json.tags.push(node.get("v"));
case "user":
json.contributors.push(node.get("name"));
case "version":
json.version = node.get("name");
json.releasenote = node.firstChild().toString();
case "description":
json.description = node.firstChild().toString();
case "depends":
var name = node.get("name");
var version = node.get("version");
if (version == null) version = "";
Reflect.setField(json.dependencies, name, version);
default:
}
default:
}
}
return json;
}
static function prettyPrint(json:Dynamic, indent="")
{
var sb = new StringBuf();
sb.add("{\n");
var firstRun = true;
for (f in Reflect.fields(json))
{
if (!firstRun) sb.add(",\n");
firstRun = false;
var value = switch (f) {
case "dependencies":
var d = Reflect.field(json, f);
prettyPrint(d, indent + " ");
default:
Json.stringify(Reflect.field(json, f));
}
sb.add(indent+' "$f": $value');
}
sb.add('\n$indent}');
return sb.toString();
}
static var sampleXml = '<project name="detox" url="https://github.com/jasononeil/detox" license="MIT">
<user name="jason"/>
<tag v="js" />
<tag v="dom" />
<tag v="jquery" />
<tag v="xml" />
<tag v="domtools" />
<tag v="dtx" />
<tag v="detox" />
<description>Detox (previously called DOMTools) - A cross-target library for Haxe to make working with Xml and the DOM easy... not dissimilar from jQuery etc but works wherever Haxe works.</description>
<version name="0.8.0.0">Rename to detox, working on JS (client), Neko, CPP and Flash9+. Major additions to Widgets, (more to come), more fixes and unit tests</version>
<depends name="xirsys_stdjs"/>
<depends name="tink_macros"/>
<depends name="beanhx" version="1.0" />
<depends name="selecthxml"/>
</project>
';
}