Chapter 8 Working with XML Documents


Document Object Model examples

This section contains two examples illustrating XML applications of Dynamo. Subsequent sections describe the techniques employed in these examples.

Converting XML to HTML for sending to a browser

This section provides an overview of how applications can use the DOM interface. It uses the DOMNode object to process an XML document.

The document

The document below is shown with white space for readability. To reproduce this example, delete the white space between the tags.

<?xml version='1.0' ?>
<Customer>
<FName>Jessie</FName>
<LName>Gagliardo</LName>
<Address>
<Street>2800 Park Avenue</Street>
<City>Hull</City>
<Region Type="Province">PQ</Region>
</Address>
</Customer>

The script

This script converts the customer information to HTML and sends it to a Web browser. The line numbers are added in comments, and are referenced in the description following the script:

<!--SCRIPT Customer.ssc                                    //1
domDoc = toDOMDocument(document.value.xmlData ); //2
if( domDoc == null ) { //3
document.writeln( site.GetErrorInfo() ); //4
} else { //5
domElement = domDoc.documentElement; // Customer node //6
document.writeln( "<P>Name: "); //7
domElement = domElement.firstChild; //8
document.writeln( domElement.firstChild.nodeValue ); //9
domElement = domElement.nextSibling; //10
document.writeln( " " + domElement.firstChild.nodeValue );//11
domElement = domElement.nextSibling; //12
document.writeln( "<P>Address: "); //13
for( i = 0; i < domElement.childNodes.length ; i ++ ){ //14
document.writeln(
domElement.childNodes.item(i).firstChild.nodeValue ); //15
} //16
} //17
-->

The output is as follows:

<P>Name: 
Jessie
Gagliardo
<P>Address:
2800 Park Avenue
Hull
PQ

How the script works

The script works as follows:

Inserting data from XML into a database

XML applications can insert data from an XML document into a database. For example, you may design a Web page to contain a form that, when a button is clicked, constructs an XML document (perhaps using JavaScript) and posts it to PowerDynamo.

This section describes how to construct an INSERT statement from an XML document.

The following XML document (which has had white space added for readability) contains information for a new product, to be added to the database:

<?xml version='1.0'?>
<Products>
<Product>
<Name>Cricket Cap</Name>
<Description>Cotton Cap</Description>
<Size>One size fits all</Size>
<Color>Blue</Color>
<Quantity>30</Quantity>
<Unit_Price>10</Unit_Price>
</Product>
</Products>

The following DynaScript code takes a DOMDocument that has been constructed from this, and creates an INSERT statement from it:

function SQLVar( prod, elemname ){                                          //1
return prod.getElementsByTagName( elemname ).item(0).firstChild.nodeValue;//2
} //3
function InsertProduct(){ //4
var i, prod, name, desc, size, color; //5
var quantity, unit_price, id, //6
var docElem, domDoc, xmlDoc; //7
domDoc = toDOMDocument(document.value.xmlData); //8
docElem = domDoc.documentElement ; //9
sql = connection.CreateQuery(); //10
id=1000; // Not a recommended way of generating ID numbers //11
// loop over individual product elements -- only one in this example //12
for( i = 0 ; i < docElem.childNodes.length ; i ++ ){ //13
id += 1; //14
prod = docElem.childNodes.item(i); //15
name = SQLVar( prod , "Name" ); //16
desc = SQLVar( prod , "Description" ); //17
size = SQLVar( prod , "Size" ); //18
color = SQLVar( prod , "Color" ); //19
quantity = SQLVar( prod , "Quantity" ); //20
unit_price = SQLVar( prod , "Unit_Price" ); //21
sql.SetSQL( "INSERT INTO DBA.Product " //22
+ "VALUES ( "
+ id + ", '" + name + "', '" + desc + "', '"
+ size + "', '" + color + "', " + quantity + ", "
+ unit_price + " )" );
if( !sql.Execute() ){ //23
document.writeln( sql.GetErrorInfo() ); //24
} //25
} //26
} //27

Notes

 


Copyright © 1999 Sybase, Inc. All rights reserved.