Solved

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

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

<xml 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'>
<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>
            <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>
            <s:AttributeType name='CBenefi' rs:number='3' rs:nullable='true'>
                  <s:datatype dt:type='string' dt:maxLength='41'/>
            </s:AttributeType>
            <s:AttributeType name='DBenPat' rs:number='4' rs:nullable='true'>
                  <s:datatype dt:type='string' dt:maxLength='40'/>
            </s:AttributeType>
            <s:AttributeType name='EBenMat' rs:number='5' rs:nullable='true'>
                  <s:datatype dt:type='string' dt:maxLength='30'/>
            </s:AttributeType>
            <s:AttributeType name='FRemite' rs:number='6' rs:nullable='true'>
                  <s:datatype dt:type='string' dt:maxLength='41'/>
            </s:AttributeType>
            <s:AttributeType name='GRemPat' rs:number='7' rs:nullable='true'>
                  <s:datatype dt:type='string' dt:maxLength='30'/>
            </s:AttributeType>
            <s:AttributeType name='HRemMat' rs:number='8' rs:nullable='true'>
                  <s:datatype dt:type='string' dt:maxLength='30'/>
            </s:AttributeType>
            <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>
            <s:AttributeType name='JMoneda' rs:number='10' rs:nullable='true'>
                  <s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='2'/>
            </s:AttributeType>
            <s:AttributeType name='KBanco' rs:number='11' rs:nullable='true' rs:writeunknown='true'>
                  <s:datatype dt:type='string' dt:maxLength='35'/>
            </s:AttributeType>
            <s:AttributeType name='LCuenta' rs:number='12' rs:nullable='true'>
                  <s:datatype dt:type='string' dt:maxLength='20'/>
            </s:AttributeType>
            <s:AttributeType name='NTPago' rs:number='13' rs:nullable='true'>
                  <s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='13'/>
            </s:AttributeType>
            <s:extends type='rs:rowbase'/>
      </s:ElementType>
</s:Schema>
<rs:data>
      <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'/>
</rs:data>
</xml>

>>>>>> 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!....
0
Comment
Question by:egwerta
  • 5
  • 5
10 Comments
 
LVL 9

Expert Comment

by:sparkplug
ID: 9763895
Hi,

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
Next

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

>S'Plug<
0
 

Author Comment

by:egwerta
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.
0
 
LVL 9

Expert Comment

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

If Not oXMLDom.LoadXML(sXMLFromADORecordset)Then

to

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

>S'Plug<
0
 
LVL 9

Expert Comment

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

>S'Plug<
0
 

Author Comment

by:egwerta
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")
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 9

Accepted Solution

by:
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")

>S'Plug<
0
 

Author Comment

by:egwerta
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...
0
 

Author Comment

by:egwerta
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...
0
 
LVL 9

Expert Comment

by:sparkplug
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

>S'Plug<

0
 

Author Comment

by:egwerta
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!
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
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 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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

895 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now