Convert oracle result set to XML using


The OracleXMLQuery class generates XML given an SQL query.

getXMLSchema()

Description

This methods generates the XML Schema(s) corresponding to the specified query; returns the XML Schema(s).

Syntax

public org.w3c.dom.Document[] getXMLSchema();

getXMLString()

Description

Transforms the object-relational data, specified in the constructor, into a XML document. Returns the string representation of the XML document. The options are described in the following table.

Syntax Description
public String getXMLString(); Takes no arguments.
public String getXMLString(int metaType); The metaType argument is used to specify the type of XML metadata the XSU is to generate along with the XML.
public String getXMLString(org.w3c.dom.Node root); If not NULL, the root argument, is considered the root element of the XML doc.
public String getXMLString(org.w3c.dom.Node root,int metaType); If not NULL, the root argument is considered the root element of the XML doc. The metaType argument is used to specify the type of XML metadata the XSU is to generate along with the XML.

Following snippet converts oracle result set to XML WHERE cs is a valid callable statement object and rs is the jdbc result set object

import oracle.xml.sql.query.OracleXMLQuery;

OracleXMLQuery qry = new OracleXMLQuery(cs.getConnection(),rs); // prepare Query class

qry.useLowerCaseTagNames();
qry.setRowsetTag(“students”);
qry.setRowTag(“student”);
qry.setRaiseNoRowsException(true);
qry.setRaiseException(true);
str = qry.getXMLString();

Above snippet will create XML in the following format

<students>

<student>

<firstname>John</firstName>

<id>2323</id>

</student>

<student>

<firstname>Mary</firstName>

<id>343</id>

</student>

</students>

we will see in the next tutorial on how to transform this generated XML to user defined XML schema definition using XSLT

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: