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…

Find Facebook Friends with Your Facebook Connect Application
Dec 28, 2009
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

Jennifer  BortelWin FREE iPhone Developer Books and Videos- Introducing @InformIT Giveaways
By Jennifer Bortel on February 5, 2010 No Comments

Apples’s recent iPad announcement made our hearts flutter so we couldn’t resist making an announcement of our own!

Today marks the first ever @InformIT Giveaway!

We’ll regularly post a video like this one profiling spectacular prizes we’re giving away—from books and videos to T-shirts and other exciting stuff. Check out the video below to see the giveaways for today, and then scroll down for more prize details and instructions on how to win them!

Dustin Sullivan"Every OSX developer should have this book on their desk."
By Dustin Sullivan on February 1, 2010 No Comments

That was the sentence Mike Riley ended his recent Dr Dobb's CodeTalk review of Cocoa Programming Developer's Handbook with.

David ChisnallCocoa Tip of the Day, 1/29/10
By David Chisnall on January 29, 2010 No Comments

Don't ignore old versions of OS X.

See All Related Blogs

Informit Network