When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles [1.x] [2.0]
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips
Search

Sections:
Book Reviews
Sample Chapters
Commonly Asked Message Board Questions
Headlines from ASPWire.com
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
Web Hosts
XML Info
Information:
Advertise
Feedback
Author an Article
Technology Jobs

















internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers
ASP ASP.NET ASP FAQs Message Board Feedback ASP Jobs
Print this page.

Windows Systems Administrator
Jupitermedia
US-CT-Darien

Justtechjobs.com Post A Job | Post A Resume

Published: Saturday, June 10, 2000

Converting Hierarchical Recordsets Into XML, Part 2
By Richard Chisholm


  • Read Part 1

  • On the ASP page you will need to create both the SQL and connection strings used in the component. This allows you to use the component across multiple pages that call different databases. If you don't know how to data shape recordsets or you need to brush up, be sure to read Scott Mitchell's great articles on the subject: Data Shaping and Advanced Data Shaping Techniques. We will be using the Northwind database provided by Microsoft. Let's take a look at the first part of the ASP page:

    - continued -

    Set objCvtXML = Server.CreateObject("cXML.Convert")
    With objCvtXML 'pass the db info
    	.Connection = "Provider=MSDataShape; Data Provider=" & _
    				  "Microsoft.Jet.OLEDB.4.0; Data Source=" & strPath
    	.SQL_String = cmdSQL
    End With
    strXMLCode = objCvtXML.Convert
    

    Note - since I used a With block, the VBScript scripting engine 5.0 or higher will be needed... if you are unsure what version of VBScript you are running be sure to check out: Determining the Server-Side Scripting Language and Version.

    First we create an instance of the component and then assign the connection, where strPath is the full path to your database, and cmdSQL is your data shaped SQL statement. Notice the data-shaping provider is included in the connection string. In the ASP file, I have included 3 SQL statements: parent-child, parent-child-grandchild, and parent-child-grandchild-great-grandchild. For this article we will use the following parent-child SQL statement:

     cmdSQL = "SHAPE {SELECT * FROM Orders WHERE OrderID > 10261 " & _ 
              "AND OrderID < 10264} As Order " & _
              "APPEND ({Select ProductID, OrderID, UnitPrice, Quantity, Discount From " & _
              "[Order Details] WHERE OrderID > 10261 AND OrderID < 10264} As Details" & _
              " RELATE OrderID TO OrderID)"
    

    Nothing special here, but note I am using the WHERE clause to limit the data to a few records. Now we are ready to get into the component and start building the document. First the XML object must be initialized:

    Set XMLDoc = Server.CreateObject("Microsoft.XMLDOM")
    With XMLDoc
        .async = False 'We do not want asynchronous downloads
        .validateOnParse = False 'don't want to validate
        .preserveWhiteSpace = False 'do not preserve white space
        .resolveExternals = False 'do not resolve external DTDs, etc
    End With
    

    This is pretty self-explanatory, but there are a few different ways to do this. If you want to validate against a schema then you can set validateOnParse to true. Additionally, to take advantage of the more current XML parsers released by Microsoft (the MSXML3 parser, which we will use on the client side later), take a look at this recent article from ASPToday, Monitor Your Web Site Performance with the XMLHTTP Component, that lists the different ProgID's and ClassID's. Following the "just in time" doctrine the database connection should not be opened until the last possible moment, meaning we can create the first portion of the XML document before opening the db connection. First the processing instruction will be created, then the root element.

    1: Set XMLInstruct = XMLDoc.createProcessingInstruction("xml", " version=""1.0""")
    2: XMLDoc.appendChild XMLInstruct
    3: Set XMLRoot = XMLDoc.createElement("Root") 'Set the value
    4: XMLDoc.appendChild XMLRoot 'And then attach it to the document.
    5: XMLRoot.setAttribute "Name", "Root" 'The name of the tag, then the value
    6: 'XMLRoot.setAttribute "xmlns", "x-schema:schema.xml"
    

    XMLInstruct is used here at the top of the document to create, as you can probably guess, the <?xml version="1.0"?> tag. The appendChild method on Line 2 is used to attach new elements/instructions to the XML document. Then we create the root element (Line 3) using the createElement(YourElementName) method. Again, to add this element to the XML document (Line 4) use appendChild, and then we can add the attributes. With the setAttribute method, the name of your attribute goes first, then the value. On Line 5, you can see I am using the Name attribute to hold my data. Line 6 attaches the schema, so that you can ensure your document is valid if you set the validateOnParse value to true. That is not necessary, but if you are planning to include a schema, here is where you do it (I have commented it out). The above code will produce:

    <?xml version="1.0"?>
    <Root Name="Root" xmlns="x-schema:schema.xml"/>
    

    Now we can open the database, get our data shaped recordsets and call the recursive function to create the bulk of the document, which we'll look at in Part 3.

  • Read Part 3 of "Converting Hierarchical Recordsets into XML"


    Windows Internet Technology | ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article



  • JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

    Solutions
    Whitepapers and eBooks
    Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
    Microsoft Article: 7.0, Microsoft's Lucky Version?
    Microsoft Article: Hyper-V--The Killer Feature in Windows Server 2008
    Avaya Article: How to Feed Data into the Avaya Event Processor
    Microsoft Article: Install What You Need with Windows Server 2008
    HP eBook: Putting the Green into IT
    Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
    Avaya Article: Setting Up a SIP A/S Development Environment
    IBM Article: How Cool Is Your Data Center?
    Microsoft Article: Managing Virtual Machines with Microsoft System Center
    HP eBook: Storage Networking , Part 1
    Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
    MORE WHITEPAPERS, EBOOKS, AND ARTICLES
    Webcasts
    Intel Video: Are Multi-core Processors Here to Stay?
    On-Demand Webcast: Five Virtualization Trends to Watch
    HP Video: Page Cost Calculator
    Intel Video: APIs for Parallel Programming
    HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
    Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
    MORE WEBCASTS, PODCASTS, AND VIDEOS
    Downloads and eKits
    Sun Download: Solaris 8 Migration Assistant
    Sybase Download: SQL Anywhere Developer Edition
    Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
    Red Gate Download: SQL Compare Pro 6
    Iron Speed Designer Application Generator
    MORE DOWNLOADS, EKITS, AND FREE TRIALS
    Tutorials and Demos
    How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
    eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
    IBM Article: Collaborating in the High-Performance Workplace
    HP Demo: StorageWorks EVA4400
    Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
    Microsoft How-to Article: Get Going with Silverlight and Windows Live
    MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES