Solved

XML VBScript Assistance?

Posted on 2011-02-15
4
468 Views
Last Modified: 2012-05-11
I am trying to extract data from a file I have in an address application.

The format of the file is:

<?xml version="1.0" encoding="iso-8859-1"?>
<FullAddressListing>
<addressSetting value="Corporate">
    <streetAddress>123
Corp Way</streetAddress>
    <city>MyCity</city>
    <stateOrProvince>MyState</stateOrProvince>
    <zipOrPostalCode>123456</zipOrPostalCode>
    <country>US</country>
  </addressSetting>
<addressSetting value="Remote1">
    <streetAddress>123
Remote Way</streetAddress>
    <city>MyCity</city>
    <stateOrProvince>MyState</stateOrProvince>
    <zipOrPostalCode>78910</zipOrPostalCode>
    <country>US</country>
  </addressSetting>
</FullAddressListing>

I am trying to use VBScript to Extract each value so I can import it into system for badging (not relevant).

I have tried different XML code snippets from this site and many others but can't get the script to dump out what I need.

I would like to set each value (address, State, Zip) as a unique varible (i.e. strState=oNode.SelectSingleNode("stateOrProvince").text but nothing seems to work.

Any assistance is appreciated.
0
Comment
Question by:TexasPlowBoy
[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
4 Comments
 

Accepted Solution

by:
bondy666 earned 250 total points
ID: 34898874
You could do something a bit like this.

Const ForReading = 1

Set fso = CreateObject("Scripting.FileSystemObject")

Set XMLReader = fso.OpenTextFile("C:\Temp\Address.xml", ForReading)
Do While Not XMLReader.AtEndOfStream
ReadXML = XMLReader.ReadLine

If InStr(1,ReadXML,"city")>0 Then
	CITY = Replace(ReadXML,"<city>",vbNullString)
	CITY = Replace(CITY,"</city>",vbNullString)
End If

If InStr(1,ReadXML,"stateOrProvince")>0 Then
	State = Replace(ReadXML,"<stateOrProvince>",vbNullString)
	State = Replace(State,"</stateOrProvince>",vbNullString)
End If

If InStr(1,ReadXML,"country")>0 Then
	country = Replace(ReadXML,"<country>",vbNullString)
	country = Replace(country,"</country>",vbNullString)
End If

If InStr(1,ReadXML,"streetAddress")>0 Then
	Street = Replace(ReadXML,"<streetAddress>",vbNullString)
	Street = Replace(Street,"</streetAddress>",vbNullString)
End If

If InStr(1,ReadXML,"zipOrPostalCode")>0 Then
	Postcode = Replace(ReadXML,"<zipOrPostalCode>",vbNullString)
	Postcode = Replace(Postcode,"</zipOrPostalCode>",vbNullString)
End If

Loop

WScript.Echo Street & vbCrLf & City & vbCrLf & State & vbCrLf & Postcode & vbCrLf & Country

Open in new window

0
 
LVL 65

Assisted Solution

by:RobSampson
RobSampson earned 250 total points
ID: 34902885
Hi, this will list the node values in the XML, but it's not clear what you want to do with each value....

Regards,

Rob.
Const NODE_TEXT = 3
Set objXML = CreateObject("Microsoft.XMLDOM")
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
strXMLFile = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "test.xml"
If objXML.load(strXMLFile) Then
	DisplayNode objXML.childNodes, 0
Else
	MsgBox "Unable to load " & strXMLFile
End If

MsgBox "Finished."

Sub DisplayNode(ByRef Nodes, ByVal Indent)
	Dim xNode
	Indent = Indent + 2

	For Each xNode In Nodes
		If xNode.nodeType = NODE_TEXT Then
			WScript.Echo Space(Indent) & xNode.parentNode.nodeName & ": " & xNode.nodeValue
		End If

		If xNode.hasChildNodes Then
			DisplayNode xNode.childNodes, Indent
		End If
	Next
End Sub

Open in new window

0
 
LVL 74

Expert Comment

by:Glen Knight
ID: 35340015
This question has been classified as abandoned and is being closed as part of the Cleanup Program. See my comment at the end of the question for more details.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
After seeing many questions for JRNL_WRAP_ERROR for replication failure, I thought it would be useful to write this article.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

726 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