How to get data from an ADO XML file and insert it on a database using ASP

Posted on 2003-11-13
Last Modified: 2013-11-19
I have this XML file:

<xml xmlns:s='uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882'
<s:Schema id='RowsetSchema'>
      <s:ElementType name='row' content='eltOnly'>
            <s:AttributeType name='ANumrecibo' rs:number='1' rs:nullable='true'>
                  <s:datatype dt:type='string' dt:maxLength='21'/>
            <s:AttributeType name='BFecha' rs:number='2' rs:nullable='true'>
                  <s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='8' rs:fixedlength='true'/>
            <s:AttributeType name='CBenefi' rs:number='3' rs:nullable='true'>
                  <s:datatype dt:type='string' dt:maxLength='41'/>
            <s:AttributeType name='DBenPat' rs:number='4' rs:nullable='true'>
                  <s:datatype dt:type='string' dt:maxLength='40'/>
            <s:AttributeType name='EBenMat' rs:number='5' rs:nullable='true'>
                  <s:datatype dt:type='string' dt:maxLength='30'/>
            <s:AttributeType name='FRemite' rs:number='6' rs:nullable='true'>
                  <s:datatype dt:type='string' dt:maxLength='41'/>
            <s:AttributeType name='GRemPat' rs:number='7' rs:nullable='true'>
                  <s:datatype dt:type='string' dt:maxLength='30'/>
            <s:AttributeType name='HRemMat' rs:number='8' rs:nullable='true'>
                  <s:datatype dt:type='string' dt:maxLength='30'/>
            <s:AttributeType name='ITotalMn' rs:number='9' rs:nullable='true'>
                  <s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='10' rs:fixedlength='true'/>
            <s:AttributeType name='JMoneda' rs:number='10' rs:nullable='true'>
                  <s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='2'/>
            <s:AttributeType name='KBanco' rs:number='11' rs:nullable='true' rs:writeunknown='true'>
                  <s:datatype dt:type='string' dt:maxLength='35'/>
            <s:AttributeType name='LCuenta' rs:number='12' rs:nullable='true'>
                  <s:datatype dt:type='string' dt:maxLength='20'/>
            <s:AttributeType name='NTPago' rs:number='13' rs:nullable='true'>
                  <s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='13'/>
            <s:extends type='rs:rowbase'/>
      <z:row ANumrecibo='390111-65' BFecha='08/11/02' CBenefi='RAMON  ' DBenPat='ORTIZ' EBenMat='MARQUEZ' FRemite='BENITO  '
             GRemPat='ORTIZ' HRemMat='CORRAL' ITotalMn='96400     ' JMoneda='mn' KBanco='BANCRECER/BANORTE' LCuenta='0987638459'
             NTPago='A Cuenta'/>
      <z:row ANumrecibo='430106-5' BFecha='08/11/02' CBenefi='MARIA  ' DBenPat='BALTAZAR' EBenMat='SANCHEZ' FRemite='MELITON  '
             GRemPat='LUNA' HRemMat='BALTAZAR' ITotalMn='96000     ' JMoneda='mn' KBanco='BANCRECER/BANORTE' LCuenta='9872536475'
             NTPago='A Cuenta'/>

>>>>>> My question is... what I need to do to get data between the <rs:data> tags (data section) in order to DEFINE VARIABLES so I can insert them into a MS Access database, using SQL and ASP...

This is the SQL statement to insert the variable on my database...

sql1 = "INSERT INTO tabla1 " &_
"(Anumrecibo, Bfecha, Cbenefi, Dbenpat, Ebenmat, Fremite, Grempat, Hremmat, "&_
" Itotalmn, Jmoneda, Kbanco, Lcuenta, Ntpago) VALUES ( " &_
" '" & Anumrecibo & "', "&_
" '" & Bfecha & "', "&_
" '" & Cbenefi & "', "&_
" '" & Dbenpat & "', "&_
" '" & Ebenmat & "', "&_
" '" & Fremite & "', "&_
" '" & Grempat & "', "&_
" '" & Hremmat) & "', "&_
" '" & Itotalmn & "', "&_
" '" & Jmoneda & "', "&_
" '" & Kbanco & "', "&_
"" '" & Lcuenta & "', "&_
" '" & Ntpago & "'  )"
Conexion.Execute (sql1)

But I haven't find yet the answer in how to get all the variables from that XML file...

Thanks for your help!....
Question by:egwerta
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
  • 5
  • 5

Expert Comment

ID: 9763895

The easiest and most efficient way to do this is to use the XML DOM. The following ASP code gives an example of how this is done where sXMLFromADORecordset is the XML from your recordset:


'Create an XML DOM document
Set oXMLDom = Server.CreateObject("MSXML2.DOMDocument.4.0")
oXMLDom.async = false

'Load XML into DOM
If Not oXMLDom.LoadXML(sXMLFromADORecordset)Then
      Set oErr = oXMLDom.parseError
      sErrMsg = "XML Parsing Error. File: " & oErr.url & "  Reason : " & oErr.reason & " Line: " & oErr.line & ", Character: " & oErr.linepos & ", Text: " & oErr.srcText
      Err.Raise 999, sErrMsg
End If

'Set Dom query language and namespaces
Call oXMLDom.setProperty("SelectionLanguage", "XPath")
Call oXMLDom.setProperty("SelectionNamespaces", "xmlns:s='uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882' xmlns:dt='uuid:C2F41010-65B3-11d1-A29F-00AA00C14882' xmlns:rs='urn:schemas-microsoft-com:rowset' xmlns:z='#RowsetSchema'")

'Get Date node
Set oDataNode=oXMLDom.selectSingleNode("/xml/rs:data")

'Iterate through each row
For Each oRowNode In oDataNode.childNodes

      sSQL = sSQL & "INSERT INTO tabla1 " &_
      "(Anumrecibo, Bfecha, Cbenefi, Dbenpat, Ebenmat, Fremite, Grempat, Hremmat, "&_
      " Itotalmn, Jmoneda, Kbanco, Lcuenta, Ntpago) VALUES ( " &_
      " '" & oRowNode.selectSingleNode("@ANumrecibo").nodeValue & "', "&_
      " '" & oRowNode.selectSingleNode("@BFecha").nodeValue & "', "&_
      " '" & oRowNode.selectSingleNode("@CBenefi").nodeValue & "', "&_
      " '" & oRowNode.selectSingleNode("@DBenPat").nodeValue & "', "&_
      " '" & oRowNode.selectSingleNode("@EBenMat").nodeValue & "', "&_
      " '" & oRowNode.selectSingleNode("@FRemite").nodeValue & "', "&_
      " '" & oRowNode.selectSingleNode("@GRemPat").nodeValue & "', "&_
      " '" & oRowNode.selectSingleNode("@HRemMat").nodeValue & "', "&_
      " '" & oRowNode.selectSingleNode("@ITotalMn").nodeValue & "', "&_
      " '" & oRowNode.selectSingleNode("@JMoneda").nodeValue & "', "&_
      " '" & oRowNode.selectSingleNode("@KBanco").nodeValue & "', "&_
      " '" & oRowNode.selectSingleNode("@LCuenta").nodeValue & "', "&_
      " '" & oRowNode.selectSingleNode("@NTPago").nodeValue & "'  )" & vbNewline

Conexion.Execute (sSQL)


You can find more information about using the XML DOM here


Author Comment

ID: 9764173
Thanks Sparkplug, this code looks fine, but I have not clear where I need to write the name of my .Xml file to load it.

Expert Comment

ID: 9764433
If you are loading from a file, change the line

If Not oXMLDom.LoadXML(sXMLFromADORecordset)Then


If Not oXMLDom.Load(Server.MapPath(sXMLFilePath))Then

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.


Expert Comment

ID: 9764451
... and replace sXMLFilePath with the path to your XML file


Author Comment

ID: 9764591
I'm getting the next error...

Server object error 'ASP 177:800401f3'
Server.CreateObject Failed

Error located in this line...
Set oXMLDom = Server.CreateObject("MSXML2.DOMDocument.4.0")

Accepted Solution

sparkplug earned 500 total points
ID: 9764785
Do you have version 4 of the MSXML parser installed on your server? If not you can get it here

Alternatively you can use version 3 by changing the line Set oXMLDom = Server.CreateObject("MSXML2.DOMDocument.4.0")  to Set oXMLDom = Server.CreateObject("MSXML2.DOMDocument.3.0") or just Set oXMLDom = Server.CreateObject("MSXML2.DOMDocument")


Author Comment

ID: 9765016
It works!!...  thanks!...
At the end of the code I just changed the "Next" after "Conexion.Execute (sSQL)"... 'cause it was inserting just the last record of the database... but that was all...
Now it's working perfect... Thanks again Sparkplug...

Author Comment

ID: 9765908
Everything was fine localy, but when I uploaded the files to the server, I'm getting this error...

msxml2.dll error '80004005'
Property name is invalid.

I'm using this line...
Set oXMLDom = Server.CreateObject("MSXML2.DOMDocument")

I think is related to the MSXML version on server rigth?... I need to contact my server provider...

Expert Comment

ID: 9769873
Thanks for the points. In reply to your final comments...

"At the end of the code I just changed the "Next" after "Conexion.Execute (sSQL)"... " 

This shouldn't have been necessary as each insert statement was being appended to the previous one. e.g. sSQL = sSQL & "...". It is more efficient to send all the insert statements to the SQL server in one go than to call Execute for each one.

"I'm getting this error...
msxml2.dll error '80004005'
Property name is invalid."

Maybe MSXML version 4 is installed on the server, in which case you should change the ProgID back to "MSXML2.DOMDocument.4.0" ref:



Author Comment

ID: 9771539
Appending insert statements is new for me... ok, thanks for this great tip!
The problem with MSXML was 'cause my server provider didn't have any version installed... rockies!... jeje.... thanks again!

Featured Post

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

734 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