Link to home
Start Free TrialLog in
Avatar of drezner7
drezner7

asked on

Parsing XML Values with VbScript

I am trying to parse out an XML file using VbScript.  I have somewhat of a code but is not writing and it is throwing an error 'wrong number of arguments '  Please help, how to read the XML correctly to pull the text from the nodes.

Dim xmlDoc, FirstTag, SecondTag

Set xmlDoc = CreateObject("Msxml2.DOMDocument")
xmlDoc.load("c:\Test\test.xml")

FirstTag = xmlDoc.getElementsByTagName("CageCode")
SecondTag = xmlDoc.getElementsByTagName("PartNumber")

Set objDialog = CreateObject("UserAccounts.CommonDialog")

sText = "C:\Test\Output.txt"

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oOut = oFSO.CreateTextFile(sText, True)


oOut.WriteLine  FirstTag
oOut.WriteLine  SecondTag


Set xmlDoc = Nothing

Thank you
Avatar of kaufmed
kaufmed
Flag of United States of America image

You've got a couple of issues. Firstly, getElementsByTagName returns an object, so you'll need a Set on lines 6 & 7. Secondly, WriteLine expects a string, but you are attempting to pass objects of type IXMLDOMNode. A modification to lines 17 & 18 is in order as well. Please see the following for my proposed changes.

Dim xmlDoc, FirstTag, SecondTag

Set xmlDoc = CreateObject("Msxml2.DOMDocument")
xmlDoc.load("test.xml")

Set FirstTag = xmlDoc.getElementsByTagName("CageCode")
Set SecondTag = xmlDoc.getElementsByTagName("PartNumber")

Set objDialog = CreateObject("UserAccounts.CommonDialog")

sText = "Output.txt"

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oOut = oFSO.CreateTextFile(sText, True)


oOut.WriteLine  FirstTag(0).text
oOut.WriteLine  SecondTag(0).text


Set xmlDoc = Nothing

Open in new window


Please note: I'm only taking the first node (i.e. the 0 in lines 17 & 18) when printing. You can loop through the nodes in both FirstTag and SecondTag (they are of type IXMLDOMNodeList) if that is your requirement. The above is meant to be an example to get you headed in the right direction. To see how to loop, you can review here:  http://msdn.microsoft.com/en-us/library/ms765549.aspx
Avatar of drezner7
drezner7

ASKER

It throws an error that in line 17: of the code  'object required: '[object]'
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you very much ... It works great. This just saved me 20 hours of work
NP. Glad to help  = )