|
Answer» Greetings all. I have just had a need to WORK with a small xml file in a vbscript program, and after a bit of head scratching I was able to do what I needed to do. However, I am wondering if there is a better way. The xml file has only one node, with four elements, three of which have an attribute called "default". I need to load the node text and the default attribute in a 2d array for later processing. I should note, that I am not even sure if I am using the correct terminology with "node", "element", and "attribute". I am hoping that someone will be able to look at the xml file, and maybe point out some areas of improvement. As I said, the objective is to load the array, *exactly* as it is being loaded, but maybe without relying on "On Error Resume Next". Thanks for any advice or tips. Code: [Select] <user_config> <ftp_map> <fname default="u:\vbscript\downloads">download</fname> <fname>pwq</fname> <fname default="u:\vbscript\downloads\somo">somotwo</fname> <fname default="u:\vbscript\downloads\mrdr">mrdr</fname> </ftp_map> </user_config>
and now the vbscript... Code: [Select] ' This array will be used to map a file name to a default ' LCD directory value for ftp later in the script Dim arElements() Set xmlDoc = CreateObject( "Microsoft.XMLDOM" ) xmlDoc.async=false xmlDoc.Load("U:\xml\xml_test.xml") set nodes = xmlDoc.selectNodes("/user_config/ftp_map/*") ' Here I want to loop through all the elements in the "ftp_map" node, ' pull out the fname value, and the default attribute, if it exists. ' The only way I COULD get it to work is by using the trusty ' "On Error Resume Next". Is there a better way to determine if ' any of the node elements are missing the "default" attribute? ReDim arElements(nodes.length,1) On Error Resume Next For i = 0 to nodes.length - 1 arElements(i ,0) = nodes(i).text ' How can I check for the one missing attribute in element "pwq"? arElements(i,1) = nodes(i).getAttribute("default") Next ' This is not PART of the solution, it is only here so I ' can verify that what I think is in the array is REALLY there. strAr = "" For i = 0 to UBound(arElements) - 1 Step 1 strAr = strAr & "fname = " & arElements(i,0) & Chr(9) & " default = " & arElements(i,1) & vbCrLf Next msgbox strAr This little snippet demos method and might give you some ideas. As written it will list the nodes that have a default attribute. You can replace this (WScript.Echo - line 10) with your existing code to load the array.
Code: [Select]Set xmlDoc = CreateObject("Microsoft.XMLDOM") xmlDoc.async = False xmlDoc.load("U:\xml\xml_test.xml")
Set colNodes = xmlDoc.selectNodes("user_config/ftp_map/*") For Each node In colNodes Set colAttribs = node.attributes For Each strAttr in colAttribs If strAttr.baseName = "default" Then WScript.Echo node.baseName & ":", node.text, strAttr.baseName & ":", strAttr.text 'replace with array load End If Next Next
Thank you Sidewinder, I like that method much better, as I'm not having to generate errors. I can see there is much much more to xml that I had thought.
[Edit] I am amazed at how perceptibly faster your solution is than having to load and read the array. Thank you so much!
|