ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Meindert" <meind...@pastelebusiness.com>
Subject RE: converting complex object to xml data file
Date Fri, 23 Mar 2007 07:27:30 GMT
Have you thought about using xstream <http://xstream.codehaus.org/>  ?

 

import com.thoughtworks.xstream.XStream;

import org.dom4j;

 

XStream xstream = new XStream();

xstream.alias("parent", Parent.class);

xstream.alias("childeren", List.class);

xstream.alias("child", Child.class);

org.dom4j.Document doc =
DocumentHelper.parseText(xstream.toXML(parentJavaOject));

 

  _____  

From: Utkarsh Ahuja [mailto:ahuja.utkarsh@gmail.com] 
Sent: 22 March 2007 11:28 PM
To: user-java@ibatis.apache.org
Subject: converting complex object to xml data file

 

Hi,
I am trying to populate a complex object and get the final result with
multiple complex objects into a single xml document directly using iBatis:

  class Parent {
     private String parentA;
     private String parentB; 
     private List children;
    
     public ....getters and setters
  }

  class Child{
     private String childA;
     private String childB;
     public ....getters and setters
  }

  <resultMap id="parentResultMap" class="com.blah.Parent">
        <result column="parent_a" property="parentA" />
        <result column="parent_b" property="parentB" /> 
        <!-- customizations -->    
        <result column="parent_a" property="children"
select="loadChildren"/>
    </resultMap>    

   <select id="loadParentObject" resultMap="parentResultMap"> 
        select * from parent
   </select>

   <resultMap id="childResultMap" class="com.blah.Child">
        <result column="child_a" property="childA" />
        <result column="child_b" property="childB" />
    </resultMap>    

  <select id="loadChildren" resultMap="childResultMap">
        select * from children
        where parent_a = #parentA:VARCHAR#
   </select>

I can get this to load the java objects however I would like to get it in
xml such as

<?xml version="1.0" encoding="UTF-8"  ?>
<parent>
       <parenta>1</parenta> 
       <parentb>tom</parentb>
       <children>
           <child>
               <childa>1</childa>
               <childb>william</childb>
            </child>
            <child>
               <childa>2</childa>
               <childb>jack</childb>
            </child>
             <child>
               <childa>3</childa>
               <childb>john</childb>
            </child>
       </children>
<parent>
<parent>
       <parenta>1</parenta>
       <parentb>tom</parentb>
       <children>
           <child>
               <childa>1</childa>
               <childb>william</childb>
            </child>
       </children>
<parent>
.......


I tried to change the result map to 

   <resultMap id="parentResultMap" class="xml" xmlName="report">
        <result column="parent_a" property="parentA" />
        <result column="parent_b" property="parentB" />
        <!-- customizations -->    
        <result column="parent_a" property="children"
select="loadChildren"/>
    </resultMap> 

But I get the result something like 

<?xml version="1.0" encoding="UTF-8"  ?><parent> .......</parent> only.

 I get the results back from one level only. It concatenated 
<?xml version="1.0" encoding="UTF-8"  ?> to every row of result returned and
does not put the child elements to reflect the relationship in the final xml
generated.
Hence each row ends up becoming a separate xml document rather than the
enitire resultSet as one XML document. 

Any pointers or suggestions would really help.

Thanks
Ahuja
   


Mime
View raw message