Last active
May 26, 2017 09:47
-
-
Save nanjizal/e3e22a252a88cc213d6b65b0d0ec7448 to your computer and use it in GitHub Desktop.
experimenting with xml -> json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-resource sample.xml@sample | |
-js _.js | |
-main Main | |
-D js-flatten | |
--next | |
-cmd echo '<!DOCTYPE html><meta charset="UTF-8"><html><body><script src="_.js"></script></body></html>' >index.html | |
-cmd open ./index.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<family> | |
<father name = "Kawasaki" test="bill" test2 = "fred">Yasuhisa</father> | |
<mother>Chizuko</mother> | |
<children> | |
<girl>Shiori</girl> | |
<boy>Yusuke</boy> | |
<boy>Kairi fred</boy> | |
</children> | |
</family> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<family> | |
<father name = "Kawasaki" test="bill" test2 = "fred">Yasuhisa</father> | |
<mother>Chizuko</mother> | |
<children> | |
<girl>Shiori</girl> | |
<boy>Yusuke</boy> | |
<boy>Kairi fred</boy> | |
</children> | |
</family> _.js:42:2 | |
out xml version="1.0" encoding="UTF-8"?incTagLast undefined _.js:97:6 | |
out fatherincTagLast family _.js:97:6 | |
out motherincTagLast family _.js:97:6 | |
out girlincTagLast children _.js:97:6 | |
out boyincTagLast children _.js:97:6 | |
out childrenincTagLast children _.js:97:6 | |
out familyincTagLast family _.js:97:6 | |
tags [family,father,mother,children,girl,boy] _.js:222:3 | |
att [-name,"Kawasaki",test,"bill",test2,"fred"] _.js:223:3 | |
contents [Yasuhisa,Chizuko,Shiori,Yusuke,Kairi fred] _.js:224:3 | |
out _.js:226:3 | |
'family':{ | |
'father': | |
'-name':'Kawasaki' | |
'test':'bill' | |
'test2':'fred' | |
'Yasuhisa', | |
'mother':'Chizuko', | |
'children':{ | |
'girl':'Shiori', | |
'boy':[ | |
'Yusuke', | |
'Kairi fred' | |
] | |
}} _.js:227:3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@:enum | |
abstract OutType( Int ) { | |
var tagOut = 0; | |
var attOut = 1; | |
var contentOut = 2; | |
var nullOut = 4; | |
} | |
class Main { static public function main():Void { new Main(); } | |
public function new(){ | |
trace( haxe.Resource.getString("sample") ); | |
parse( haxe.Resource.getString("sample") ); | |
} | |
var incTags: Array<String>; | |
var out: String = ''; | |
var end: String = ''; | |
var outTag: String = ''; | |
var finalOut: String = ''; | |
var indentStr: String = ' '; | |
var indent: String = ''; | |
var indentCount: Int = 0; | |
var indentLen: Int = 3; | |
var tags: Array<String>; | |
var att: Array<String>; | |
var contents: Array<String>; | |
var str : String = ''; | |
var pos: Int; | |
var c: Int; | |
var l: Int; | |
var last: String; | |
var lastOut: OutType = nullOut; | |
var tempOut: String; | |
var tempCount: Int; | |
public function parse( str_: String ){ | |
att = new Array<String>(); | |
tags = new Array<String>(); | |
contents = new Array<String>(); | |
incTags = new Array<String>(); | |
str = str_; | |
pos = 0; | |
l = str.length; | |
c = nextChar(); | |
indentCount = 0; | |
finalOut = ''; | |
out = ''; | |
tempOut = ''; | |
tempCount = 0; | |
var b = new StringBuf(); | |
var s: String; | |
var i: Int = 0; | |
var spaces: Int = 0; | |
while( pos < l ){ | |
switch( c ){ | |
case '\n'.code, '\r'.code: | |
case '<'.code: | |
s = b.toString(); | |
if( s != '' && spaces != s.length ){ | |
contents[ i ] = s; | |
lastOut = contentOut; | |
tempCount++; | |
if( tempCount > 1 ) | |
{ | |
incIndent(); | |
tempOut = indent + tempOut + '\n' + indent; | |
} | |
tempOut += "'" + s + "',"; | |
i = contents.length; | |
} | |
spaces = 0; | |
b = new StringBuf(); | |
extractTag(); | |
default: | |
if( c == ' '.code ) spaces++; // keeps track of spaces added | |
b.addChar( c ); | |
} | |
c = nextChar(); | |
} | |
if( tempCount > 0 ){ | |
if( tempCount > 1 ){ | |
out += '[\n' + removeLastChar( tempOut ) + '\n' + indent + ']' + '\n'; | |
} else { | |
out += removeLastChar( tempOut ) + '\n'; | |
} | |
tempCount = 0; | |
tempOut = ''; | |
} | |
if( end != '' ) { | |
decIndent(); | |
out += '\n' + indent + '}'; | |
end = ''; | |
} | |
trace( 'tags ' + tags ); | |
trace( 'att ' + att ); | |
trace( 'contents ' + contents ); | |
finalOut += out + '}'; | |
trace( 'out ' ); | |
trace( finalOut ); | |
} | |
inline function removeLastChar( s: String ){ | |
return s.substr( 0, s.length-1 ); | |
} | |
inline function removeFirstLast( s: String ){ | |
return s.substr( 1, s.length-2 ); | |
} | |
inline function extractTag() { | |
c = nextChar(); | |
var b = new StringBuf(); | |
var s: String; | |
if( c == '?'.code || c == '/'.code ) { | |
c = nextChar(); | |
while( c != '>'.code ){ | |
b.addChar( c ); | |
c = nextChar(); | |
} | |
s = b.toString(); | |
//out+='dec ' + s; | |
trace( 'out ' + s + 'incTagLast ' + incTags[incTags.length-1] ); | |
if( s == incTags[incTags.length-1] ){ | |
incTags.pop(); | |
end += '\n' + indent + '}'; | |
//decIndent(); | |
} | |
return; | |
} | |
b = new StringBuf(); | |
var i = tags.length; | |
var tagStored = false; | |
while( true ) { | |
switch( c ) {//, ' '.code | |
case '>'.code: | |
if( !tagStored ){ | |
s = b.toString(); | |
if( last != s ) { | |
tags[i] = s; | |
if( lastOut == tagOut ){ | |
//out+= '\n'; | |
//out += '[' + tempOut + ']' + '\n'; | |
//tempOut = ''; | |
} else if( lastOut == contentOut ){ | |
if( tempCount > 1 ){ | |
out += '[\n' + removeLastChar( tempOut ) + '\n]' + ',\n'; | |
} else { | |
out += tempOut + '\n'; | |
} | |
tempCount = 0; | |
tempOut = ''; | |
} | |
// | |
if( lastOut == tagOut ) { | |
incIndent(); | |
out += '{\n'; | |
incTags[ incTags.length ] = tags[i-1]; | |
} | |
lastOut = tagOut; | |
out += indent + "'" + s + "':"; | |
} else { | |
//out += "\n";//+indent+"["; | |
} | |
last = s; | |
} | |
/* | |
trace('lastOut ' + lastOut ); | |
var z = lastOut; | |
finalOut += '$z{\n'+out; | |
out = ''; | |
*/ | |
break; | |
case ' '.code: | |
tagStored = true; | |
s = b.toString(); | |
if( last != s ) { | |
tags[i] = s; | |
if( lastOut == tagOut ) { | |
incIndent(); | |
out += '{\n'; | |
incTags[ incTags.length ] = tags[i-1]; | |
} | |
lastOut = tagOut; | |
out += indent + "'" + s + "':\n"; | |
// finalOut += '{\n'+out; | |
// out = ''; | |
} else { | |
//decIndent(); | |
//out += " pie \n";//+indent+"["; | |
} | |
last = s; | |
//indent = indent + indentStr; | |
extractAtt(); | |
default: | |
b.addChar( c ); | |
} | |
tagStored = false; | |
c = nextChar(); | |
} | |
} | |
inline function incIndent(){ | |
indent = indent + indentStr; | |
} | |
inline function decIndent(){ | |
indent = indent.substr(0, indent.length - indentLen ); | |
} | |
inline function extractAtt(){ | |
var b = new StringBuf(); | |
var s: String; | |
var i = att.length; | |
var toggle: Bool = true; | |
while( true ) { | |
switch( c ) { | |
case '='.code, ' '.code: | |
s = b.toString(); | |
if( s == 'name' ) s = '-name'; | |
if( s != '' ){ | |
if( toggle ){ | |
att[i] = s; | |
lastOut = attOut; | |
out += indent + "'" + s + "':"; | |
} else { | |
att[i] = s; | |
lastOut = attOut; | |
out += "'" + removeFirstLast( s ) + "'\n"; | |
} | |
toggle = !toggle; | |
i = att.length; | |
} | |
b = new StringBuf(); | |
case '>'.code: | |
s = b.toString(); | |
att[i] = s; | |
lastOut = attOut; | |
out += "'" + removeFirstLast( s ) + "'\n"; | |
pos--; | |
break; | |
default: | |
b.addChar( c ); | |
} | |
c = nextChar(); | |
} | |
} | |
inline function nextChar() { | |
return StringTools.fastCodeAt( str, pos++ ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
{
'family': {
'-name': 'Kawasaki',
'father': 'Yasuhisa',
'mother': 'Chizuko',
'children': {
'girl': 'Shiori'
'boy': [
'Yusuke',
'Kairi'
]
}
}
};