buymeapc Posted July 22, 2011 Share Posted July 22, 2011 (edited) Hi all, I'm trying to figure out how to display the contents of an XML file in a treeview. I've been scouring the forums and found lots of examples for basic XML files, but not too much for one that's a bit more complicated as the one I have is. How can I go about displaying the values and attributes of an XML file such as this? Here's just a snippet of the XML... <configuration> <configSections> <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"> <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"> <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" /> <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"> <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere" /> <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" /> <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" /> <section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" /> </sectionGroup> </sectionGroup> </sectionGroup> </configSections> <connectionStrings> .... .... Here's what I have so far. I know I'm not very close to what I need, but I guess I just need some help to get the ball rolling. expandcollapse popup#include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <_XMLDOMWrapper.au3> #include <array.au3> Global $file_path = @WorkingDir&"\web.config" _GUI() Func _GUI() Local $msg $hGUI = GUICreate("Web.Config Sample", 800, 800) $main_tree = GUICtrlCreateTreeView(10, 10, 390, 780) _AddTreeItems($main_tree) GUISetState(@SW_SHOW) ; Run the GUI until the dialog is closed While 1 $msg = GUIGetMsg() If $msg = $GUI_EVENT_CLOSE Then ExitLoop WEnd GUIDelete() EndFunc Func _AddTreeItems($tree) _XMLFileOpen($file_path); <--Open the config file $kids = _XMLGetChildNodes("/") If IsArray($kids) = 0 Then MsgBox(48, "", "Could not get main node!") Return EndIf $main_node_count = $kids[0]; <--Main node count $main_node = $kids[1]; <--Name of the main node For $n = 1 To $main_node_count Do Sleep(100) $val = _GetChildNodes($main_node) Until $val = 1 ;~ $kids = _XMLGetChildNodes($main_node) ;~ _ArrayDisplay($kids) ;~ $Author = _XMLGetAttrib('(//Display)', 'Author') ;~ $Title = _XMLGetAttrib('(//Display)', 'Title') ;~ $FileName = _XMLGetAttrib('(//Song)', 'FilePath') ;~ ConsoleWrite($Author & @CRLF) ;~ ConsoleWrite($Title & @CRLF) ;~ ConsoleWrite($FileName & @CRLF) Next EndFunc Func _GetChildNodes($node) Local $kids $kids = _XMLGetChildNodes($node) If $kids[0] = 0 Then Return 1; <--If there are no more child nodes, exit loop Else _ArrayDisplay($kids) For $n = 1 To $kids[0] ;$all_Attribs = _XMLGetAllAttrib($kids[$n] $name = _XMLGetAttrib($kids[$n], "name") $type = _XMLGetAttrib($kids[$n], "type") $value = _XMLGetAttrib($kids[$n], "value") ;MsgBox(0, $kids[$n], $name&" "&$type&" "&$value) ConsoleWrite($kids[$n]&" "&$name&" "&$type&" "&$value&@CRLF) $another_node = _XMLGetChildNodes($kids[$n]) If $another_node[0] <> 0 Then _GetChildNodes($kids[$n]) Next Return $kids EndIf EndFunc Thank you for all your help as always! Edited July 22, 2011 by buymeapc Link to comment Share on other sites More sharing options...
twitchyliquid64 Posted July 23, 2011 Share Posted July 23, 2011 I think there is a script posted in the example section that can parse XML files. Use the forum search. It would be up to you to use this, and then use the builtin GUITreview functions to add elements to the treeview. ongoing projects:-firestorm: Largescale P2P Social NetworkCompleted Autoit Programs/Scripts: Variable Pickler | Networked Streaming Audio (in pure autoIT) | firenet p2p web messenger | Proxy Checker | Dynamic Execute() Code Generator | P2P UDF | Graph Theory Proof of Concept - Breadth First search Link to comment Share on other sites More sharing options...
buymeapc Posted July 27, 2011 Author Share Posted July 27, 2011 Ok, some progress... The script displays the xml nodes and attributes nicely, but doesn't bother continuing once it hits the node with the "xmlns" attribute. How can I get this namespace attribute to display in the output like the rest do? #include <_XMLDOMWrapper.au3> $xml = _XMLFileOpen (@ScriptDir & "\web.config") If @error Then ConsoleWrite(_XMLError()&@lf) Exit EndIf Walk() Func Walk($path = ".") $a = _XMLGetChildNodes($path) If IsArray($a) Then For $x = 1 To $a[0] If $x > 1 And $a[$x] <> $a[$x - 1] Then Dim $num = 0 $num += 1 Else $num = $x EndIf ConsoleWrite($path & "/" & $a[$x] & "[" & $num & "]" & @LF) Dim $at[1], $av[1] $b = _XMLGetAllAttrib($path & "/" & $a[$x] & "[" & $num & "]", $at, $av) If Not @error Then If IsArray($b) Then For $y = 0 To UBound($at) - 1 ConsoleWrite(StringFormat("\tAttribute::%s=%s\n", $at[$y], $av[$y])) Next EndIf EndIf Walk($path & "/" & $a[$x] & "[" & $num & "]") Next EndIf EndFunc Here's the snippet of the XML I'm using: <?xml version="1.0" encoding="utf-8"?> <configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="System.Web.Extensions" publicKeyToken="0000000000000000" /> <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Web.Extensions.Design" publicKeyToken="0000000000000000" /> <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0" /> </dependentAssembly> </assemblyBinding> </runtime> </configuration> Thanks!! Link to comment Share on other sites More sharing options...
buymeapc Posted July 28, 2011 Author Share Posted July 28, 2011 Ok, I think I've got it. Since I'm going to be displaying several XMLs of different varieties, I need to be able to show any node that contains an "xmlns" attribute as is. Basically, I just read the contents of the original file into a temp file and modify the contents so the node can be displayed with the "xmlns" attribute. Probably not the cleanest or best way, but it works. I'm always open to suggestions, though! expandcollapse popup#include <_XMLDOMWrapper.au3> #include <Array.au3> $file = @ScriptDir & "\web.config" $temp_file = $file & ".temp.xml" $temp_data = StringReplace(FileRead($file), "xmlns", "xmlns1") FileWrite($temp_file, $temp_data) $xml = _XMLFileOpen($temp_file) If @error Then ConsoleWrite(_XMLError()&@lf) Exit EndIf Walk() FileDelete($temp_file) Func Walk($path = ".") $a = _XMLGetChildNodes($path) If IsArray($a) Then For $x = 1 To $a[0] If $x > 1 And $a[$x] <> $a[$x - 1] Then Dim $num = 0 $num += 1 Else $num = $x EndIf ConsoleWrite($path & "/" & $a[$x] & "[" & $num & "]" & @LF) Dim $at[1], $av[1] $b = _XMLGetAllAttrib($path & "/" & $a[$x] & "[" & $num & "]", $at, $av) If Not @error Then If IsArray($b) Then For $y = 0 To UBound($at) - 1 If StringInStr($at[$y], "xmlns1") <> 0 Then $at[$y] = StringReplace($at[$y], "xmlns1", "xmlns") ConsoleWrite(StringFormat("\tAttribute::%s=%s\n", $at[$y], $av[$y])) Next EndIf EndIf Walk($path & "/" & $a[$x] & "[" & $num & "]") Next EndIf EndFunc Here's the XML snippet: <?xml version="1.0" encoding="utf-8"?> <configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="System.Web.Extensions" publicKeyToken="0000000000000000" /> <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Web.Extensions.Design" publicKeyToken="0000000000000000" /> <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0" /> </dependentAssembly> </assemblyBinding> </runtime> </configuration> Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now