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

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
  • 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 http://msdn.microsoft.com/library/en-us/xmlsdk/htm/dom_devguide_overview_2g1j.asp?frame=true


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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.


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 http://www.microsoft.com/downloads/details.aspx?FamilyId=3144B72B-B4F2-46DA-B4B6-C5D7485F2B42&displaylang=en.

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: http://p2p.wrox.com/archive/xml/2002-05/19.asp



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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Suggested Solutions

Title # Comments Views Activity
ASP.NET reading ATOM 2 68
VB.Net. Reading xml value 6 29
xslt 1.0 - How to split value 8 27
XSL FO - remove empty space and automatically size columns 6 11
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
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:
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

856 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