本文来自于Thinkai’s Blog,thinkai也是我在ahk上面的引路人,对thinkai感兴趣的朋友请关注他的博客。
thinkai的博客简洁、有趣,即便是转载过来我也尽量保持作者原有的风格,主要是方便大家查阅。
thinkai的博客简洁、有趣,即便是转载过来我也尽量保持作者原有的风格,主要是方便大家查阅。
;示例 str = ( George John Reminder Don't forget the meeting! ) xml_root_node := XA_LoadStr(str) ;从字符串加载xml 从文件用XA_Load(Path) xml_obj := %xml_root_node% ;数组对象变量名是返回的根节点 MsgBox % xml_obj.to "`n" "`n<" xml_root_node ">`n" . XA_ArrayToXML(xml_obj) . "`n" XA_Save(Array, Path) { FileDelete, % Path FileAppend, % "`n<" . Array . ">`n" . XA_ArrayToXML(Array) . "`n", % Path, UTF-8 If (ErrorLevel) Return 1 Return 0 } XA_LoadStr(XMLText) { Local XMLObj, XMLRoot, Root1, Root2 XMLObj := XA_LoadXML(XMLText) XMLObj := XMLObj.selectSingleNode("/*") XMLRoot := XMLObj.nodeName %XMLRoot% := XA_XMLToArray(XMLObj.childNodes) Return XMLRoot } XA_Load(Path) { Local XMLText, XMLObj, XMLRoot, Root1, Root2 If (!FileExist(Path)) Return 1 FileRead, XMLText, % Path XMLObj := XA_LoadXML(XMLText) XMLObj := XMLObj.selectSingleNode("/*") XMLRoot := XMLObj.nodeName %XMLRoot% := XA_XMLToArray(XMLObj.childNodes) Return XMLRoot } XA_XMLToArray(nodes, NodeName="") { Obj := Object() for node in nodes { if (node.nodeName != "#text") ;NAME { If (node.nodeName == "Invalid_Name" && node.getAttribute("ahk") == "True") NodeName := node.getAttribute("id") Else NodeName := node.nodeName } else ;VALUE Obj := node.nodeValue if node.hasChildNodes { ;Same node name was used for multiple nodes If ((node.nextSibling.nodeName = node.nodeName || node.nodeName = node.previousSibling.nodeName) && node.nodeName != "Invalid_Name" && node.getAttribute("ahk") != "True") { ;Create object If (!node.previousSibling.nodeName) { Obj[NodeName] := Object() ItemCount := 0 } ItemCount++ ;Use the supplied ID if available If (node.getAttribute("id") != "") Obj[NodeName][node.getAttribute("id")] := XA_XMLToArray(node.childNodes, node.getAttribute("id")) ;Use ItemCount if no ID was provided Else Obj[NodeName][ItemCount] := XA_XMLToArray(node.childNodes, ItemCount) } Else Obj.Insert(NodeName, XA_XMLToArray(node.childNodes, NodeName)) } } return Obj } XA_LoadXML(ByRef data){ o := ComObjCreate("MSXML2.DOMDocument.6.0") o.async := false o.LoadXML(data) return o } XA_ArrayToXML(theArray, tabCount=1, NodeName="") { Local tabSpace, extraTabSpace, tag, val, theXML, root tabCount++ tabSpace := "" extraTabSpace := "" if (!IsObject(theArray)) { root := theArray theArray := %theArray% } While (A_Index < tabCount) { tabSpace .= "`t" extraTabSpace := tabSpace . "`t" } for tag, val in theArray { If (!IsObject(val)) { If (XA_InvalidTag(tag)) theXML .= "`n" . tabSpace . "" . XA_XMLEncode(val) . "" Else theXML .= "`n" . tabSpace . "<" . tag . ">" . XA_XMLEncode(val) . "" } Else { If (XA_InvalidTag(tag)) theXML .= "`n" . tabSpace . "" . "`n" . XA_ArrayToXML(val, tabCount, "") . "`n" . tabSpace . "" Else theXML .= "`n" . tabSpace . "<" . tag . ">" . "`n" . XA_ArrayToXML(val, tabCount, "") . "`n" . tabSpace . "" } } theXML := SubStr(theXML, 2) Return theXML } XA_InvalidTag(Tag) { Char1 := SubStr(Tag, 1, 1) Chars3 := SubStr(Tag, 1, 3) StartChars := "~``!@#$%^&*()_-+={[}]|:;""'<,>.?/1234567890 `n`r" Chars := """'<>=/ `n`r" Loop, Parse, StartChars { If (Char1 = A_LoopField) Return 1 } Loop, Parse, Chars { If (InStr(Tag, A_LoopField)) Return 1 } If (Chars3 = "xml") Return 1 Return 0 } XA_XMLEncode(Text) { StringReplace, Text, Text, &, &, All StringReplace, Text, Text, <, <, All StringReplace, Text, Text, >, >, All StringReplace, Text, Text, ", ", All StringReplace, Text, Text, ', ', All Return Text }
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。