Created
May 29, 2017 10:01
-
-
Save triplefox/1db0fcaf6dbb0e58a8d9d5a286b8d864 to your computer and use it in GitHub Desktop.
Flattens all the nuances of Haxe's Xml implementation into an enum, where it's much easier to capture data.
This file contains hidden or 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
import Xml; | |
/* Flattens all the nuances of Haxe's Xml implementation | |
into an enum, where it's much easier to capture data. */ | |
enum FlatXMLNode { | |
Element( | |
name : String, | |
attribute : Map<String,String>, | |
children : Array<FlatXMLNode>); | |
PCData(s : String); | |
CData(s : String); | |
Comment(s : String); | |
ProcessingInstruction(s : String); | |
Document( | |
children : Array<FlatXMLNode>); | |
} | |
class FlatXML { | |
public static function toFlat(x : Xml) : FlatXMLNode { | |
switch(x.nodeType) { | |
case Element: | |
var am = new Map<String,String>(); | |
for (k in x.attributes()) | |
am.set(k,x.get(k)); | |
return Element( | |
x.nodeName, | |
am, | |
[for (n in x) toFlat(n)] | |
); | |
default: | |
throw 'help, i am unequipped for nodeType ${x.nodeType}'; | |
case PCData: | |
return PCData(x.nodeValue); | |
case CData: | |
return CData(x.nodeValue); | |
case Comment: | |
return Comment(x.nodeValue); | |
case ProcessingInstruction: | |
return ProcessingInstruction(x.nodeValue); | |
case Document: | |
return Document([for (n in x) toFlat(n)]); | |
} | |
} | |
public static function fromFlat(f : FlatXMLNode) : Xml { | |
switch(f) { | |
case Element(name, attribute, children): | |
var x = Xml.createElement(name); | |
for (a in attribute.keys()) { | |
x.set(a, attribute.get(a)); | |
} | |
for (c in children) | |
x.addChild(fromFlat(c)); | |
return x; | |
case PCData(s): | |
return Xml.createPCData(s); | |
case CData(s): | |
return Xml.createCData(s); | |
case Comment(s): | |
return Xml.createComment(s); | |
case ProcessingInstruction(s): | |
return Xml.createProcessingInstruction(s); | |
case Document(children): | |
var x = Xml.createDocument(); | |
for (c in children) | |
x.addChild(fromFlat(c)); | |
return x; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment