Solved

Parsing XML Values with VbScript

Posted on 2011-09-30
5
493 Views
Last Modified: 2012-05-12
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
0
Comment
Question by:drezner7
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36892100
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
0
 

Author Comment

by:drezner7
ID: 36892451
It throws an error that in line 17: of the code  'object required: '[object]'
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 36892543
You may want to check for NULL before trying to use the values:

If Not FirstTag Is Nothing Then
    oOut.WriteLine  FirstTag(0).text
End If

If Not SecondTag Is Nothing Then
    oOut.WriteLine  SecondTag(0).text
End If

Open in new window

0
 

Author Closing Comment

by:drezner7
ID: 36892575
Thank you very much ... It works great. This just saved me 20 hours of work
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36892595
NP. Glad to help  = )
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question