Home > Articles > Databases > SQL Server

Serve SQL Data in XML Format

Ivan Pepelnjak
  • PrintPrint
  • Share ThisShare This
  • DiscussDiscuss
Close WindowIvan Pepelnjak

Ivan Pepelnjak

Learn more…

Publish Your Application Stories to Facebook
Oct 21, 2009
Use Facebook Connect to Bring Your Application to Millions of Users
Aug 31, 2009
Developing AJAX Applications with jQuery
Jun 30, 2009
Web Page Refactoring with jQuery
Jun 10, 2009
Introduction to jQuery: Use jQuery to Write Simpler, Shorter, More Readable JavaScript Code
Jun 1, 2009
Testing Your Website in a Realistic Environment
Aug 5, 2008
Fix Your Web Site Performance Problems
Jul 30, 2008
Why Is My Web Site So Slow for Global Visitors?
Jul 21, 2008
Generating Atom Feed from SQL Data
Mar 31, 2008
Implementing Access Controls on SQL Server Data
Mar 14, 2008
Make Your Web Pages Mobile-Friendly
Jan 18, 2008
Use Google as Your Gateway to the Mobile Internet
Dec 28, 2007
Enhance Your AJAX Applications with Visual Cues
Dec 14, 2007
Building Modal Dialogs into Your Web Application
Nov 9, 2007
Adding Keyboard Shortcuts to Your Web User Interface
Nov 2, 2007
XML Handling in Microsoft SQL Server 2005
Oct 19, 2007
Storing XML Data in a Relational Database
Oct 5, 2007
Serve SQL Data in XML Format
Sep 28, 2007
Enrich Your HTML Tables with JavaScript Progressive Enhancement
Sep 7, 2007
Introduction to HIJAX
Aug 24, 2007
Seven Common CSS Mistakes and How to Avoid Them
Jul 20, 2007
JavaScript Progressive Enhancement in Practice
Jun 15, 2007
Make Pop-Up Windows Visible to Search Engines
Feb 16, 2007
Automate the Pagination of Your Web Pages
Feb 2, 2007
Using WordProcessingML to Generate Clean HTML from Word
Jan 26, 2007
Optimized Presentation of XML Content
Dec 15, 2006
Reap the Benefits of Web Caching, Part 3: Database Integration
Nov 22, 2006
Reap the Benefits of Web Caching, Part 2: Reduce the Download Time
Nov 17, 2006
Reap the Benefits of Web Caching, Part 1: Explicit Content Expiration
Oct 27, 2006
Improve Your Search Engine Ranking with AJAX
Jul 28, 2006
Using Multicast Domains
Jun 27, 2003
Should I Be Interested in MPLS Traffic Engineering?
Jan 3, 2003
Is Troubleshooting Important?
Dec 27, 2002
MPLS/VPN Architecture Overview
Aug 2, 2002
Multiprotocol Label Switching (MPLS) Architecture Overview
Jan 1, 2001
JavaScript Modal Dialog Box in a Frameset Environment
By on December 12, 2007 1 Comment

Only a few modifications are needed to adapt the solution I've described in the Building Modal Dialogs into Your Web Application to a frameset environment.

Ivan Pepelnjak describes a few ways to extract data from SQL databases and serve it to an AJAX application running in a web browser.

If you’re developing AJAX applications, you might decide to use XML as the encoding method to transport application data between the web server and the browser—not surprisingly, as XML is the technology that contributed the X in AJAX. Quite often, the data you have to serve to the client will reside in SQL databases on the server, so you need an adapter between the tabular binary SQL data and text-oriented hierarchical XML data. As you’ll see in this article, you can implement this adapter in a number of ways, depending on the SQL database you use and the implementation flexibility you need.

Before we get into the implementation details, let’s review the XML representation possibilities.

XML Representation Options

You can represent an SQL row as an XML tag with an attribute for each column value; or as an XML tag with child tags, one for each column. Listing 1 shows how a record from a Categories table is represented as a tag with attributes. Listing 2 shows the tag with the child tags format.

Listing 1 SQL row as XML attributes.

<Category CategoryId="HTML" CategoryName="HTML (Hypertext Markup Language)" />

Listing 2 SQL row as XML child tags.

<Category>
 <CategoryId>HTML</CategoryId>
 <CategoryName>HTML (Hypertext Markup Language)</CategoryName>
</Category>

Obviously, you can also mix the two; some columns could be represented as attributes and others as child tags (see Listing 3). Furthermore, you might want to use shorter names for XML tags and attributes, to cut down on response size and bandwidth utilization (see Listing 4).

Listing 3 Mixed XML format.

<Category CategoryId="HTML">
 <CategoryName>HTML (Hypertext Markup Language)</CategoryName>
</Category>

Listing 4 Optimized XML output.

<cat id="HTML">
 <name>HTML (Hypertext Markup Language)</name>
</cat>

Choosing the XML format is primarily a matter of convenience if you’re simply dumping SQL tables into XML. (The attribute notation results in slightly shorter XML code.) However, if you want to implement more complex structures, you have to consider the following issues:

  • If your XML data contains HTML or XML fragments, you might want to represent them as child tags to simplify the client-side processing. In this case, at least the columns with HTML or XML fragments should be represented as child tags.
  • If your XML response represents hierarchical data (for example, invoice with a header and a variable number of lines), you should use the hierarchical XML structure, not the flattened tabular version produced by an SQL join.
  • Share ThisShare This
  • Your Account

Discussions

Make a New Comment

You must log in in order to post a comment.

Related Resources

Danny KalevMinutes from the October 2009 Meeting
By Danny Kalev on November 19, 2009 No Comments

The minutes from the Santa Cruz (October 2009) meeting are available here. Even if you're not a language layer at heart, I encourage you to read them.

Danny KalevA Reader's Opinion on Attributes
By Danny Kalev on October 20, 2009 No Comments

In August I dedicated a series to the debate about C++0x attributes. I believe that it covered the subject in a balanced and detailed way, but I keep getting complaints from C++ users who don't like attributes for various reasons. Here's a recent email I received from a Polish C++ programmer. While it  doesn't represent my opinion about attributes -- I'm rather neutral about this feature and consider it a "solution waiting for a problem" -- but it suggests that attributes are still a highly controversial issue that will haunt C++ for a long time. The email is quoted here with minor edits that and as usual, with all private details removed.

Danny KalevFollowup: The Web 2.0 Guy I Ain't
By Danny Kalev on October 16, 2009 1 Comment

Almost a year ago, I posted here The Web 2.0 Guy I Ain't. People wonder whether I still resist all those Web 2.0 features and technologies at the end of 2009.

See All Related Blogs

Informit Network