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

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!....
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


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

egwertaAuthor Commented:
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.
If you are loading from a file, change the line

If Not oXMLDom.LoadXML(sXMLFromADORecordset)Then


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

Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

... and replace sXMLFilePath with the path to your XML file

egwertaAuthor Commented:
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")
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")


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
egwertaAuthor Commented:
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...
egwertaAuthor Commented:
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...
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:


egwertaAuthor Commented:
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!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.