Spry

Dynamically generating XML

Frequently, users want to generate XML using information from a database, a method that’s useful for obtaining data when the data changes frequently. Spry can accept data from an XML page or any page that can generate valid XML. You can do this, for instance, using ASP, PHP, or ColdFusion pages and application servers. Adobe provides many code samples that show you how to convert database queries into valid XML. For more information, see https://github.com/adobe/Spry/tree/master/samples/utils/query2xml.html.

To dynamically generate XML using server-side functionality, you should be familiar with the concept of content types. A content type tells the browser what kind of content is on the page. By default, the content type for web pages is set to text/html. This content type tells the browser that it can expect HTML code from the server, and that the page requires no additional processing. This is the most common content type that’s set for HTML pages.

If you are dynamically generating XML content and sending it to a browser using a server-side technology, however, you must tell the browser that the content you are serving is actually XML. To do this, you need to specify a content type in your dynamic page.

Following are some examples of content type headers for PHP, ASP, and ColdFusion pages:
PHP
header('Content-type: text/xml')

ASP
Response.ContentType = "text/xml"

ColdFusion
<cfcontent type="text/xml" reset="Yes">

Note: Some server-side languages require that you set any header data at the very top of the script.

For examples of the full code, see https://github.com/adobe/Spry/tree/master/samples/utils/query2xml.html.

Specifying a content type ensures the browser’s recognition of the data as XML. If you don’t specify a content type, or if the content type is incorrect (for example, text/html), the browser recognizes the text as plain text and might not display the data properly. When you create a dynamic source for a Spry page, you can test to see whether or not your XML is valid by viewing the XML or dynamic page in a browser. If the page displays an XML tree in the browser, the XML works for Spry. If the XML displays as a mass of text, the content type is probably incorrect.
Note: In certain situations Internet Explorer 7 will not display the XML tree correctly, even if the XML will work for Spry.

One other concern with dynamically generated XML is making sure that the browser (or server) does not cache the data. Browser or server caching can prevent your most recent data from reaching Spry. To prevent this, specify HTTP headers for the following items:

  • Cache-Control

  • Expires

  • Pragma

Consult the preceding link for examples of the full code.