Chapter 8 Working with 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:
ResultsToXMLString
method.toXMLString
function.The built-in methods for producing XML documents are discussed in this section.
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".
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
ResultsToXMLString
takes
between zero and four parameters. For details, see "ResultsToXMLString
method" in PowerDynamo Reference.<Products>
<Product>
<ID>600</ID>
<Name>Sweatshirt</Name>
<Price>24.00</Price>
</Product>
<Product>
<ID>601</ID>
<Name>Sweatshirt</Name>
<Price>24.00</Price>
</Product>
</Products>
There are several points of interest in the above example.
GetErrorCode
method
of the object carrying out the step. Many methods return null when
they fail.<?xml version='1.0' encoding='ISO-8859-1'?>
ResultsToXMLString
,
the name of the column is used as the tag for each tag. If you supply
a single tag, it is used for all columns. If you supply more than
one tag, but not as many as there are columns, then the remaining
columns tags default to the column labels.ResultsToXMLString
method
provides a convenient way of supplying simple XML. To specify attributes
for XML tags in your output, see "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>";
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>
<!--data-->
tags,
then the attributes do not apply.You can create XML from any DynaScript object
using the toXMLString
function.
This function accepts 1, 2, or 3 arguments:
toXMLString
function returns null if
a non-object argument is passed as the first argument, or if the
object passed has no enumerable members. Otherwise it returns a string.
The following set of scripts illustrate how
to use toXMLString
:
<!--SCRIPT
myObj = null;
myObj.a = 2;
myObj.b = 3;
x = toXMLString( myObj );
document.writeln(x);
-->
<a>2</a><b>3</b>
<!--SCRIPT
//Create a nested object
myObj = null;
myObj.a = 2;
myObj.b.b1 = 3;
myObj.b.b2 = 4;
x = toXMLString( myObj );
document.writeln(x);
-->
<a>2</a><b><b1>3</b1><b2>4</b2></b>
<!--SCRIPT
// Using '&' character escaping" )
myObj = null;
myObj.a = 2;
myObj.b = "&<encoding needed>&";
x = toXMLString( myObj );
document.writeln(x);
-->
<a>2</a><b>&<encoding needed>&</b>
toXMLString
.
<!--SCRIPT
document.writeln( "***** Using CDATA character escaping" );
myObj = null;
myObj.a = 2;
myObj.b = "&<encoding needed>&";
x = toXMLString( myObj, true );
document.writeln(x);
-->
<a>2</a><b><![CDATA[&<encoding needed>&]]></b>
<!--SCRIPT
// Indexed members with no tags specified"
myObj = null;
myObj[0] = 2;
myObj[1] = 3;
x = toXMLString( myObj, false );
document.writeln(x);
-->
<0>2</0><1>3</1>
<!--SCRIPT
// Indexed members, with tags specified" );
myObj = null;
row0 = null;
row1 = null;
row0[0] = "shirt";
row0[1] = "blue";
row1[0] = "hat";
row1[1] = "green";
myObj[0] = row0;
myObj[1] = row1;
x = toXMLString( myObj, false, "ROW COL" );
document.writeln(x);
-->
<ROW><COL>shirt</COL><COL>blue</COL></ROW><ROW><COL>hat</COL><COL>green</COL></ROW>
Copyright © 1999 Sybase, Inc. All rights reserved. |