Chapter 8 Working with XML Documents


Creating XML documents

In PowerDynamo, an XML document is any string that contains a valid XML document. Once you have created an XML document, you can send it to a browser, or parse it and process it using the DynaScript DOM objects.

You can create XML documents in PowerDynamo from a variety of sources:

The built-in methods for producing XML documents are discussed in this section.

Creating XML documents from the query object

If you use the DynaScript query object to execute a SQL query against your database, you can use the ResultsToXMLString method to wrap the results in XML tags.

For an alternative approach using tags, see "Creating XML documents from queries using the FORMATTING tag".

Example

The following script illustrates how to use ResultsToXMLString :

myConn = site.GetConnection( "myConn" );         //1
myQuery = myConn.CreateQuery(
"SELECT id, name, unit_price FROM product" ); //2
xmlString = myQuery.ResultsToXMLString(
"Products",
"Product",
"ID Name Price" ); //3
document.writeln( xmlString ); //4

Notes

There are several points of interest in the above example.

Creating custom XML documents from query result sets

You can customize the XML you obtain from the result set of a query object by building an XML string explicitly, instead of using ResultsToXMLString .

For example:

xmlString = "<Products>";
while ( myQuery.MoveNext() ){
xmlString += "<Product>";
xmlString += "<ID>" + myQuery.GetValue(1) + "</ID>" ;
xmlString += "<Name>" + myQuery.GetValue(2) + "</Name>";
xmlString += '<Price units="DOLLARS">' + myQuery.GetValue(3) + "</Price>";
xmlString += "</Product>";
}
xmlString += "</Products>";

Creating XML documents from queries using the FORMATTING tag

The FORMATTING tag provides a way of writing out the results of queries as XML.

The following template illustrates how to use the FORMATTING tag with XML output. You must enter the FORMATTING tag, like any tag, all on one line in a Dynamo template:

<!--SQL
SELECT description, color
FROM product
WHERE unit_price > 15
-->
<!--FORMATTING FORMAT_AS="XML" RESULTSET_TAG="Products" ROW_TAG="Product" COLUMN_TAG="Description Color"-->
<!--/FORMATTING-->

The output of this template is the following. Line wrapping occurs in the printed document but is not present in the actual output string:

<Products>
<Product>
<Description>Hooded Sweatshirt</Description>
<Color>Green</Color>
</Product>
<Product>
<Description>Zipped Sweatshirt</Description>
<Color>Blue</Color>
</Product>
</Products>

Notes

Creating XML from DynaScript objects

You can create XML from any DynaScript object using the toXMLString function.

This function accepts 1, 2, or 3 arguments:

Examples

The following set of scripts illustrate how to use toXMLString :

 


Copyright © 1999 Sybase, Inc. All rights reserved.