Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parse XML file with VB Script 2

Status
Not open for further replies.

AppSpecialist

Programmer
Jul 6, 2001
64
CA
I am receiving a data file produced in XML format. I am using ASP and VB Script and want to read this XML file and write some of the contents out to a database. There are approximately 15 records in this file so I would need to loop through the contents to write all the records.

Does anyone have a snippit of code I could look at that would provide insight on how to do this? A simple, generic solution would be ideal.

Thanks
 
The article suggested can probably help... but let me be more specific with some psuedo-code...

Access the XML document
Read 3 fields of the many provided
Do until all occurrences have been read
Move 3 fields to variables
Do some manipulation on them
Write 3 fields to a SQL Server database
Loop

The parts I am interested in is how to first access the XML file through VB Script. Parse it to gain access to the information stored in the 3 fields. Then loop until the complete file has been read.

Thanks
 
Hi,

you can use the MSXMLDOM to access the xml file and then something like this:

Code:
Dim oDomdocument

Set oDomdocument = Server.CreateObject("MSXML2.DomDocument")

'sXML is
oDomdocument.Load("the fileref to your xml file here")

.
.
.
.

Set oDomdocument = Nothing

Where you see the dots, you can query the domdocument to retrieve the nodes you require and write the values to the database.

Take a look at the methods of the domdocument in the msdn to see what works best for you. e.g. SelectNodes vs. ChildNodes or another method.

Good luck!

Jordi Reineman
 
Hello,
Jordi is right.
I'll try to provide some code to test.
=================
catalog.xml
=================
<catalog>
<book id=&quot;bk101&quot;>
<author>Mat Who</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
<book id=&quot;bk102&quot;>
<author>Kim Rall</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description> ... become queen of the world.</description>
</book>
</catalog>

=================
catalog.asp
=================

<%
'Load the XML
set oXmlDoc = Server.CreateObject(&quot;MSXML2.DOMdocument&quot;)
oXmlDoc.async = false
oXmLDoc.load(Server.MapPath(&quot;catalog.xml&quot;))

'Declare lists of authors, titles and years
Dim objAuthorList
Dim objTitleList
Dim objPublishDate

'Set the values from XML file, assumed that each author wrote only one book
'getElementsByTagName returns a collection of elements that have the specified name
Set objAuthorList = oXmlDoc.getElementsByTagName(&quot;author&quot;)
Set objTitleList = oXmlDoc.getElementsByTagName(&quot;title&quot;)
Set objPublishDate = oXmlDoc.getElementsByTagName(&quot;publish_date&quot;)

Response.write &quot;<html><head></head><body>&quot; & vbCrlf
'Display some output
'length as usual is the number of elements in a collection
'object.item(i) is a way to access i-th element of a collection
'text is a property of an item; other are nodeName, nodeValue
For i=0 To (objAuthorList.length -1)
str = objAuthorList.item(i).text & &quot; wrote &quot; & objTitleList.item(i).text & &quot; in year &quot; & Mid(objPublishDate.item(i).text,1,4) & &quot;<br>&quot; & vbCrLf
Response.write str
Next
Response.Write &quot;</body></html>&quot;
%>

From this example you could figure out how to write necessary data to a database.
D.
 
&quot;From this example you could figure out how to write necessary data to a database.
D.&quot;

How so, D? I didn't know that MSXML2.DOMdocument had a write function. Doesn't your Response.Write simply display to the screen?
 
Hello,
Yep, Response.Write simply diplay to the screen.
The question was how to have some specific data from XML file in an ASP variable.
That I had shown.
I assumed that frankflynn knows how to use ADO Connection, Recordset, etc. to put the variable values into database.
And that I meant by &quot;From this example you could figure out how to write necessary data to a database&quot;.
:)
D.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top