Java Reference
In-Depth Information
Depending on the structure of the
XML
that you are starting with, it could be eas-
ier to use
XSL
to transform it into the structure required by i
BATIS
than to process
the
XML
and turn it into a Java object, which is your alternative in those cases.
6.1.2
XML results
The i
BATIS
framework also allows you to create
XML
results from mapped state-
ments. When running a mapped statement that returns
XML
, you get a complete
XML
document for each returned object.
To use this feature, you create a mapped statement that has a result class of
xml
. Here is a simple example:
<select id="getByIdValueXml" resultClass="xml"
xmlResultName="account">
select
accountId,
username,
password
from Account
where accountId = #value#
</select>
String xmlData = (String) sqlMap.queryForObject(
"Account.getByIdValueXml",
new Integer(1));
The results returned in this case will look like this (well, not exactly; we added
some whitespace and line feeds to make it more readable):
<?xml version="1.0" encoding="UTF-8"?>
<account>
<accountid>1</accountid>
<username>lmeadors</username>
<password>blah</password>
</account>
Getting that data back is real handy if you have a single record that you want to get
as an
XML
document. If you want to get multiple objects, you can do that as well:
<select id="getAllXml" resultClass="xml" xmlResultName="account">
select
accountId,
username,
password,
firstName,
lastName,
address1,
address2,
city,
