Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 830
  • Last Modified:

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'
      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
egwerta
Asked:
egwerta
  • 5
  • 5
1 Solution
 
sparkplugCommented:
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
 
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.
0
 
sparkplugCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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

>S'Plug<
0
 
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")
0
 
sparkplugCommented:
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
 
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...
0
 
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...
0
 
sparkplugCommented:
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
 
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!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now