Created
September 22, 2012 11:54
-
-
Save maestrith/3765936 to your computer and use it in GitHub Desktop.
AHK XML Parser
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
#SingleInstance, Force | |
gui:=new xml("gui","Wierd","acid.xml") | |
another:=new xml("another") | |
gui.add("Just_A_Path") | |
gui.add("Path","","With text") | |
gui.add("unique",{value:3},"",1) | |
gui.add("unique",{value:3},"",1) | |
gui.add("unique",{value:4},"",1,{value:3}) | |
for a,b in {gui:gui,another:another}{ | |
b.add("foo",{this:1}) | |
b.add("foo/bar/another",{this:1},"Hi") | |
b.add("foo/bar",{another:1}) | |
b.add("foo",{that:1},"Hello",1) | |
} | |
m(gui[],another[]) | |
gui.transform() | |
another.transform() | |
m(gui[],another[]) | |
gui.save() | |
return | |
class xml{ | |
__New(param*){ | |
ref:=param.1,root:=param.2,file:=param.3 | |
file:=file?file:ref ".xml",root:=!root?ref:root | |
temp:=ComObjCreate("MSXML2.DOMDocument"),doc.setProperty("SelectionLanguage","XPath") | |
ifexist %file% | |
temp.load(file),this.xml:=temp | |
else | |
this.xml:=xml.CreateElement(temp,root) | |
this.file:=file | |
} | |
__Get(){ | |
return this.xml.xml | |
} | |
CreateElement(doc,root){ | |
x:=doc.CreateElement(root),doc.AppendChild(x) | |
return doc | |
} | |
add(path,att="",text="",dup="",find=""){ | |
main:=this.xml.SelectSingleNode("*") | |
for a,b in find | |
if found:=main.SelectSingleNode("//" path "[@" a "='" b "']"){ | |
for a,b in att | |
found.setattribute(a,b) | |
return found | |
} | |
if p:=this.xml.SelectSingleNode(path) | |
for a,b in att | |
p.SetAttribute(a,b) | |
else | |
{ | |
p:=main | |
Loop,Parse,path,/ | |
{ | |
total.=A_LoopField "/" | |
if dup | |
new:=this.xml.CreateElement(A_LoopField),p.AppendChild(new) | |
else if !new:=p.SelectSingleNode("//" Trim(total,"/")) | |
new:=this.xml.CreateElement(A_LoopField),p.AppendChild(new) | |
p:=new | |
} | |
for a,b in att | |
p.SetAttribute(a,b) | |
if Text | |
p.text:=text | |
} | |
} | |
remove(){ | |
this.xml:="" | |
} | |
save(){ | |
this.xml.save(this.file) | |
} | |
transform(){ | |
this.xml.transformNodeToObject(xml.style(),this.xml) | |
} | |
style(){ | |
xsl := ComObjCreate("MSXML2.DOMDocument") | |
style = | |
( | |
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> | |
<xsl:output method="xml" indent="yes" encoding="UTF-8"/> | |
<xsl:template match="@*|node()"> | |
<xsl:copy> | |
<xsl:apply-templates select="@*|node()"/> | |
<xsl:for-each select="@*"> | |
<xsl:text></xsl:text> | |
</xsl:for-each> | |
</xsl:copy> | |
</xsl:template> | |
</xsl:stylesheet> | |
) | |
xsl.loadXML(style), style:=null | |
return xsl | |
} | |
} | |
m(x*){ | |
for a,b in x | |
list.=b "`n" | |
MsgBox,% list | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment