Java Reference
In-Depth Information
from Account
List xmlList = sqlMap.queryForList("Account.getAllXml", null);
The resulting list in this case is a list of XML documents. Just what you wanted,
right? Well, in some cases, maybe—but in most cases, no. Instead of a single XML
document with multiple account elements in it, you get a list of strings like our pre-
vious result, which means that you have to manipulate the strings if you want to
concatenate them all together into a single document. This is not exactly optimal.
The workaround is to not use i BATIS to get the XML results. A simple approach
is to use a normal i BATIS mapped statement that returns a collection and creates
the XML from that. One way to do that (if you are using a bean for your results) is
to create a method like the following to help create the XML :
public String toXml(){
StringBuffer returnValue = new StringBuffer("");
returnValue.append("<accountid>" + getAccountId() +"</accountid>");
returnValue.append("<username>" + getUsername() + "</username>");
returnValue.append("<password>" + getPassword() + "</password>");
return returnValue.toString();
Another approach to this issue is to create a class that uses reflection to convert a
bean to XML . This is a fairly simple exercise. Here is a small utility that will get you
started on this. Although this code is abbreviated to save some space, it demon-
strates the technique.
public class XmlReflector {
private Class sourceClass;
private BeanInfo beanInfo;
private String name;
XmlReflector(Class sourceClass, String name) throws Exception {
this.sourceClass = sourceClass; = name;
beanInfo = Introspector.getBeanInfo(sourceClass);
public String convertToXml(Object o) throws Exception {
StringBuffer returnValue = new StringBuffer("");
if (o.getClass().isAssignableFrom(sourceClass)) {
PropertyDescriptor[] pd = beanInfo.getPropertyDescriptors();
Search WWH ::

Custom Search