Results 1 to 9 of 9

Thread: DB Record To XML

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2002
    Location
    Montreal Canada
    Posts
    40

    DB Record To XML

    Hello All

    Does anyone have an "easy" way to convert a database records into an XML style file.

    Ex. If I have a table Customer having fields FirstName, LastName,Address, Phone. I'd want to convert that to something like :

    <Cusotmer>
    <FirstName>Paul</FirstName>
    <LastName>Smith</LastName>
    <Address>555 Somewhere</Address>
    <Phone>555-5555</Phone>
    </Customer>
    ....
    do this for each customer record in my database.

    Once the file is made and FTP'd I'd use this to update a database in another location that is looking at this file. Is this feasable or is there a better way?

    Thanks in Advance

  2. #2
    Banned jhermiz's Avatar
    Join Date
    Jun 2002
    Location
    Antarctica
    Posts
    2,492

    Re: DB Record To XML

    I thought DTS allowed you to export from a table to XML format?

    What kind of RDBMS are you using ?

    Jon

  3. #3
    Banned jhermiz's Avatar
    Join Date
    Jun 2002
    Location
    Antarctica
    Posts
    2,492

    Re: DB Record To XML

    Im thinking ahead sorry..yukon???

    It aint gonna be so easy because of the fact that you need more than one record to be outputted.

    How do you expect to display 2 records ?

    Why not use linked servers?
    What about bcp / dts ? You can move data much easier with these.

    XML is nice (see woka's nice example on language support) however you dont need to go through all this work of outputting to XML and re importing to another db. Especially if you are using SQL Server. If you are using access..what about linked tables?

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    Are you using SQL Server 2000? If so, you can configure it so that the result is in XML format..... which you should be able to load into an XML Dom document and save to the harddrive..... having never done that, I don't know how it would work.... but....

    you oculd loop through each record, for each field, and write out hte XML yourself... something like this:
    VB Code:
    1. Dim fld As ADODB.Field
    2.  
    3.   xml = xml & "<Customers>"
    4. Do While Not rs.EOF
    5.   xml = xml & "<Customer>"
    6.   For Each fld In rs.Fields
    7.     xml = xml & "<" & fld.name & ">" & fld.value & "</" & fld.Name & ">"
    8.   Next
    9.   xml = xml & "</Customer>"  rs.MoveNext
    10. Loop
    11.   xml = xml & "</Customers>"
    12. 'open file and save the XML....

    TG
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2002
    Location
    Montreal Canada
    Posts
    40
    I actually came up with the same solution as techgnome. The problem i forsee is reconverting it from XML back to db records.

    The database in question is indeed Access. Boss is tooooo cheap for anything else.

    This is my scenario:

    We have a software sitting in a head office type company. This company has x number of affiliates. The software generates reports on certain customers that are common to the affiliates. This report gets generated once a week.

    We have another software installed in the affiliate offices that already download other files daily. I was trying to get this existing software to now download this XML type file so that I can convert it and repopulate their databases. Sounds wacky, I know, but most of it is built except for reconverting the XML to DB records.

    Thx for the help jhermiz, techgnome

  6. #6
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196
    You could create a little utility prog in .NET which takes in the db and table names and outputs an xml file.
    There are some great classes in .Net for doing this The dataset class has GetXML and GetXMLSchema methods.

  7. #7
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961
    The ADO Recordset can export itself to a file in XML format:

    Save Method (ADO Recordset)


    Saves (persists) the Recordset in a file.

    Syntax

    recordset.Save FileName, PersistFormat

    Parameters

    FileName Optional. Complete path name of the file where the Recordset is to be saved.

    PersistFormat Optional. A PersistFormatEnum value that specifies the format in which the Recordset is to be saved. Can be one of the following constants.

    Constant Description
    adPersistADTG (Default) Save in proprietary Advanced Data Tablegram format.
    adPersistXML Save in XML format.


    Remarks

    The Save method can only be invoked on an open Recordset. Use the Open method to later restore the Recordset from FileName.

    If the Filter property is in effect for the Recordset, then only the rows accessible under the filter are saved. If the Recordset is hierarchical, then the current child recordset and its children are saved, but not the parent recordset.

    The first time you save the Recordset, specify FileName. If you subsequently invoke Save, omit FileName or else a run-time error will occur. If you subsequently invoke Save with a new FileName, the Recordset is saved to the new file. However, the new file and the original file will both be open.

    Save does not close Recordset or FileName, so you can continue to work with the Recordset and save your most recent changes. FileName remains open until the Recordset is closed, during which time other applications can read but not write to FileName.

    For reasons of security, the Save method permits only the use of low and custom security settings from a script executed by Microsoft Internet Explorer. For a more detailed explanation of security issues, see the whitepaper titled, "Security Issues in the Microsoft Internet Explorer," at http://www.microsoft.com/data/techmat.htm.

    If the Save method is called while an asynchronous Recordset fetch, execute, or update operation is in progress, then Save waits until the asynchronous operation is complete.

    When the Save method is done, the current row position will be the first row of the Recordset.

    For best results, set the CursorLocation property to adUseClient with Save. If your provider does not support all of the functionality necessary to save Recordset objects, the client cursor will provide that functionality.

  8. #8

    Thread Starter
    Member
    Join Date
    Nov 2002
    Location
    Montreal Canada
    Posts
    40
    Thanks for the info Dave Sell.

    This should help. I will try a few test projects. Any chance you know about the reverse. From an xml file to an Access database. I did find some excellent code using a recursive function to get a complete XML file into a tree view control. Very fast and very good but nothing to a recordset in a database.

    Thanks again.

  9. #9
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961
    Unfortunately, the XML output probably won't populate a Tree control as you might expect. I have heard the XML generated is quite ugly and horribly proprietary, very specific to the Recordset object.

    There is a reverse function, allowing a Recordset to load an XML file generated by another Recordset, probably called the Load member function, used similarily, see MSDN.

    Dave

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width