Welcome!

PowerBuilder Authors: Dan Joe Barry, Ian Thain, Yakov Werde, Paul Slater, Bruce Armstrong

Related Topics: PowerBuilder, XML

PowerBuilder: Article

Integrated Support for XML Data Management

XML data management in Adaptive Server Enterprise 12.5

XML Processing in ASE
XML processing in ASE is done with XQL and the ResultSetXml class.

XQL
With XQL (the XML query language), SQL data in a database can be represented as an XML document. XQL is implemented with the com.sybase.xml.xql.Xql class. The XQL class is the interface to the XML query engine.

Parse (String xmlDoc)
‘xmlDoc’ is a String representing an XML document. The return type of the parse() method is SybXmlStream, which may be used to query a document with XQL. For example, an XML document stored in a database table (XMLDAT) column (xmldoc) can be updated with the parse() method as shown below.

update XMLDAT
set xmldoc=com.sybase.xml.xql.Xql.parse(“<xmldoc></xmldoc>”)

parse(InputStream xml_document, boolean validate)

  • ‘xml_document’ is an InputStream representing an XML document.
  • If ‘validate’ is true an XML document is validated with a specified DTD.

The return type of the parse() method is SybXmlStream, which can be used to query a document with XQL. For example, an XML document stored in a database table (XMLDAT) column (xmldoc) can be updated with the parse() method as shown below.

update XMLDAT
set xmldoc=com.sybase.xml.xql.Xql.parse(new FileInputStream(“file.xml”), true);

setParser(String parserName)

  • The setParser() method specifies the parser for the parse() method.
  • The default parser is xerces-1.3.1

query(String query, String xmlDoc)

  • ‘query’ is a String representing an XML element or attribute. The parameter ‘xmlDoc’ is the XML document being queried. The return type of the query() method is a String.

For example, an element or an attribute can be retrieved from an XML document as shown here.

String result=Xql.query(“catalog/journal”, “<xml>...</xml>”);

query(String query, InputStream xmlDoc)

  • ‘query’ is a String representing an XML element or attribute. ‘xmlDoc’ is an InputStream representing an XML document. The return type of the query() method is a String.

For example, an element or an attribute can be retrieved from an XML document.

String result=Xql.query(“/catalog/journal”, “new FileInputStream(“file.xml”)”);

ResultSetXml Class
A ResultSetXml class is a subclass of the JXML class. An XML document can be generated from a SQL result set with the xml.resultset.ResultSetXml class. The ResultSetXml class is also used to modify the XML document generated from a SQL result set. The modified ResultSetXml object can be converted to SQL data in the database. A ResultSetXml object is created from an XML ResultSet document or SQL query. An XML ResultSet document is an XML document that validates with the XML ResultSet DTD. The XML ResultSet DTD is illustrated in Listing 1.

Generating ResultSetXml from an XML ResultSet Document
A ResultSetXml object can be generated from an XML ResultSet document.

xml.resultset.ResultSetXml rsx = new xml.resultset.ResultSetXml(“XmlDocument”);

  • ‘XmlDocument’ is a String variable containing an XML ResultSet document.

The ResultSetXml(“Xml ResultSet document”) constructor also validates the XML ResultSet document.

Generating ResultSetXml from a SQL Result Set
With the ResultSetXml class an XML ResultSet document can be generated from a SQL query as shown here.

xml.resultset.ResultSetXml  rsx =
     new xml.resultset.ResultSetXml (query, cdata
Columns, colNames, server);

  • query is an SQL query that returns a result set.
  • cdataColumns indicates which columns should be XML CDATA sections.
  • colNames indicates if the resulting XML should specify “name” attribute in the “Columns” element.
  • server specifies the Adaptive server on which to execute the query.

The ResultSetXml class has methods to access and update the elements of an XML document and can be used to validate an XML result set document with the XML ResultSet DTD.

String getColumn(int rowNumber, int columnNumber)

  • ‘rowNumber’ is the index of a row in the result set.
  • ‘columnNumber’ is the index of a column in the result set.

For example, Select rsx>>getColumn(3,4) returns the String value of the fourth column in the third row. rsx is a ResultSetXml variable.

String getColumn(int rowNumber, int columnName)

  • ‘rowNumber is the index of a row in the result set.
  • ‘columnNumber’ is the name of a column in the result set. For example, Select rsx>>getColumn(3, “columnName”) returns the String value of the ‘columnName’ coulmn in the third row.rsx is a ResultSetXml variable.

void setColumn(int rowNumber, int columnNumber, newValue)

  • ‘rowNumber’ and ‘columnNumber’ are the same as for getColumn() methods.
  • The setColumn() methods sets the value of the specified column to ‘newValue’.

For example, Select rsx=rsx>>setColumn(3,4,”new value”) sets the column value of the column in the fourth column and the third row to ‘new value.’

void setColumn(int rowNumber, String columnName, newValue)

  • ‘rowNumber’ and ‘columnNumber’ are as specified for getColumn() methods.
  • The setColumn() method sets the value of the specified column name to ‘newValue.’

For example, select rsx=rsx>>setColumn(3, “columnName”, “new value”) sets the column value of ‘columnName’ column in the third row to ‘new value.’

Conclusion
The Adaptive Server Enterprise 12.5 has integrated support for XML data management.

XML data can be stored in a database and data in a database can be converted into an XML document. XQL and ResultSetXml classes are used to store, index, and query XML data in a database.

More Stories By Deepak Vohra

Deepak Vohra is a Sun Certified Java 1.4 Programmer and a Web developer.

More Stories By Ajay Vohra

Ajay Vohra is a senior solutions architect with DataSynapse Inc.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.