Solved

Get values of an attibute for all elements in an XML file (using VBscript)

Posted on 2009-04-13
3
637 Views
Last Modified: 2012-05-06
How can I get the values of an attibute for an element in an XML file using VBscript.
0
Comment
Question by:ENTPF
[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
  • 2
3 Comments
 
LVL 14

Expert Comment

by:yehudaha
ID: 24132099
read this:

http://www.devguru.com/technologies/xml_dom/15891.asp

xml file in my example:

<staff>
   <employee ssn="123456" pay="3">
      <f_name>John</f_name>
      <l_name>Sullivan</l_name>
   </employee>
   <employee ssn="987654" pay="2">
      <f_name>Mary</f_name>
      <l_name>Lopez</l_name>
   </employee>
</staff>

output:

staff
employee
f_name
l_name
employee
f_name
l_name

Set objXMLDoc = CreateObject("Microsoft.XMLDOM")
objXMLDoc.async = False
objXMLDoc.load("c:\staff.xml")
 
Set NodeList = objXMLDoc.getElementsByTagName("*")
For Each Elem In NodeList
   WScript.Echo Elem.tagName 
Next

Open in new window

0
 

Author Comment

by:ENTPF
ID: 24132390
what I need is to get the value for the 'ssn' attribute.  I need it to return '123456' ( in your example). How can this be coded using vbscript?
0
 
LVL 14

Accepted Solution

by:
yehudaha earned 500 total points
ID: 24132462
ok

here:
Set objXMLDoc = CreateObject("Microsoft.XMLDOM")
objXMLDoc.load("c:\staff.xml")
 
Set objFirstChild = objXMLDoc.documentElement.firstChild
Set objAttributes = objFirstChild.attributes
For Each Attribute in objAttributes
   WScript.Echo Attribute.name & " Value: " & Attribute.value 
Next

Open in new window

0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

739 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