You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-java@ibatis.apache.org by netsql <ce...@gmail.com> on 2007/03/16 03:36:25 UTC

ibatis to return xml instead of arraylist (of hashmap).

In the past I allways returned arraylist of hashmap and that works EXCLENT.

I now want to return xml. I saw some notes on xstream and it works but not
so well, I do not like the xml it makes.

Is there snipets or more ideas to help me?

tia,
.V

Re: ibatis to return xml instead of arraylist (of hashmap).

Posted by Clinton Begin <cl...@gmail.com>.
Hi Vic,

I think XStream is awesome, and I can't imagine what you wouldn't like about
the XML it creates....

I guess I'd suggest writing your own XML serializer...?

Clinton

On 3/15/07, netsql <ce...@gmail.com> wrote:
>
> In the past I allways returned arraylist of hashmap and that works
> EXCLENT.
>
> I now want to return xml. I saw some notes on xstream and it works but not
> so well, I do not like the xml it makes.
>
> Is there snipets or more ideas to help me?
>
> tia,
> .V
>

Re: ibatis to return xml instead of arraylist (of hashmap).

Posted by Ashish Kulkarni <as...@gmail.com>.
Hi
I would be interested in the solution you provided where i can create XML
file from HashMap.
I was using ibtaits and type=XML , but hit an issue when there are some
German characters like umlaut in the database,
here is a email i had send, but did not got any reply for it
http://www.nabble.com/Error-When-creating-XML-file-from-Database-with-Character-conversion-error-tf3409773.html#a9499524

So i want to develop some code where in i can convert HashMap to XML String.

I am not sure how do i call the code you have specified,  for example i
would do the following to get the HashMap,
But when do i call this code MapToXmlRowHandler?

Here is what i would do
in java
   List list = client.queryForList("getFooter4", new BigDecimal(33333));
in ibatis
<select id="getFooter4" resultClass="java.util.HashMap" parameterClass="
java.math.BigDecimal" >
        SELECT P704SPEC, P704LINE, P704DETL, P704USER, P704DATE, P704TIME
         FROM PU27041 WHERE P704SPEC = #value#
</select>

But where do i write this code to convert HashMap into XML

Regards
Ashish
On 3/16/07, netsql <ce...@gmail.com> wrote:
>
> No, I have not tried that yet.
> I was doing something similar to what Larry showed.
> But now I am switching to type=XML.
>
> I did not like having to jack w/ Xstream re-mapping.
> THANK YOU!
> .V
> ps: and congrats on the book, I saw it at a bookstore. If anyone goes to
> SF for Javaone or otherwise, ping me at vic at pointcast dot com.
>
> On 3/15/07, Clinton Begin <cl...@gmail.com> wrote:
> >
> > Vic,
> >
> > I'm assuming you've already tried <resultMap ... type="xml"> to get a
> > list of XML documents back?
> >
> > Clinton
> >
> > On 3/15/07, Larry Meadors <lmeadors@apache.org > wrote:
> > >
> > > Vic,
> > >
> > > You could write a row handler to take the returned maps and a string
> > > builder to create xml.
> > >
> > > One class would handle all of your requirements.
> > >
> > > Something like this is close - testing is an exercise left to the
> > > reader. ;-)
> > >
> > > ===
> > > public class MapToXmlRowHandler
> > >         implements RowHandler
> > > {
> > >     private String wrapper;
> > >     private String item;
> > >     private StringBuilder builder;
> > >
> > >
> > >     public MapToXmlRowHandler(String wrapper, String item) {
> > >         this.wrapper = wrapper;
> > >         this.item = item;
> > >         builder = new StringBuilder();
> > >     }
> > >
> > >     public void handleRow(Object object) {
> > >         Map<String, Object> m = (Map<String, Object>) object;
> > >
> > >         builder.append("<").append(item).append(">");
> > >
> > >         for(String key : m.keySet()){
> > >             // todo: make sure these values are xml friendly
> > >             builder.append ("<").append(key).append(">")
> > >                    .append(m.get(key))
> > >                    .append("</").append(key).append(">");
> > >         }
> > >
> > >         builder.append ("</").append(item).append(">");
> > >     }
> > >
> > >     public String getResultsAsXml(){
> > >         StringBuilder result = new StringBuilder("<" + wrapper + ">")
> > >                 .append(builder)
> > >                 .append("</").append(wrapper).append(">");
> > >         return result.toString();
> > >     }
> > >
> > > }
> > > ===
> > >
> > > Larry
> > >
> > >
> > > On 3/15/07, netsql < cekvenich@gmail.com> wrote:
> > > > In the past I allways returned arraylist of hashmap and that works
> > > EXCLENT.
> > > >
> > > > I now want to return xml. I saw some notes on xstream and it works
> > > but not
> > > > so well, I do not like the xml it makes.
> > > >
> > > > Is there snipets or more ideas to help me?
> > > >
> > > > tia,
> > > > .V
> > > >
> > >
> >
> >
>

Re: ibatis to return xml instead of arraylist (of hashmap).

Posted by netsql <ce...@gmail.com>.
No, I have not tried that yet.
I was doing something similar to what Larry showed.
But now I am switching to type=XML.

I did not like having to jack w/ Xstream re-mapping.
THANK YOU!
.V
ps: and congrats on the book, I saw it at a bookstore. If anyone goes to SF
for Javaone or otherwise, ping me at vic at pointcast dot com.

On 3/15/07, Clinton Begin <cl...@gmail.com> wrote:
>
> Vic,
>
> I'm assuming you've already tried <resultMap ... type="xml"> to get a list
> of XML documents back?
>
> Clinton
>
> On 3/15/07, Larry Meadors <lm...@apache.org> wrote:
> >
> > Vic,
> >
> > You could write a row handler to take the returned maps and a string
> > builder to create xml.
> >
> > One class would handle all of your requirements.
> >
> > Something like this is close - testing is an exercise left to the
> > reader. ;-)
> >
> > ===
> > public class MapToXmlRowHandler
> >         implements RowHandler
> > {
> >     private String wrapper;
> >     private String item;
> >     private StringBuilder builder;
> >
> >
> >     public MapToXmlRowHandler(String wrapper, String item) {
> >         this.wrapper = wrapper;
> >         this.item = item;
> >         builder = new StringBuilder();
> >     }
> >
> >     public void handleRow(Object object) {
> >         Map<String, Object> m = (Map<String, Object>) object;
> >
> >         builder.append("<").append(item).append(">");
> >
> >         for(String key : m.keySet()){
> >             // todo: make sure these values are xml friendly
> >             builder.append ("<").append(key).append(">")
> >                    .append(m.get(key))
> >                    .append("</").append(key).append(">");
> >         }
> >
> >         builder.append ("</").append(item).append(">");
> >     }
> >
> >     public String getResultsAsXml(){
> >         StringBuilder result = new StringBuilder("<" + wrapper + ">")
> >                 .append(builder)
> >                 .append("</").append(wrapper).append(">");
> >         return result.toString();
> >     }
> >
> > }
> > ===
> >
> > Larry
> >
> >
> > On 3/15/07, netsql < cekvenich@gmail.com> wrote:
> > > In the past I allways returned arraylist of hashmap and that works
> > EXCLENT.
> > >
> > > I now want to return xml. I saw some notes on xstream and it works but
> > not
> > > so well, I do not like the xml it makes.
> > >
> > > Is there snipets or more ideas to help me?
> > >
> > > tia,
> > > .V
> > >
> >
>
>

Re: ibatis to return xml instead of arraylist (of hashmap).

Posted by Clinton Begin <cl...@gmail.com>.
Vic,

I'm assuming you've already tried <resultMap ... type="xml"> to get a list
of XML documents back?

Clinton

On 3/15/07, Larry Meadors <lm...@apache.org> wrote:
>
> Vic,
>
> You could write a row handler to take the returned maps and a string
> builder to create xml.
>
> One class would handle all of your requirements.
>
> Something like this is close - testing is an exercise left to the reader.
> ;-)
>
> ===
> public class MapToXmlRowHandler
>         implements RowHandler
> {
>     private String wrapper;
>     private String item;
>     private StringBuilder builder;
>
>
>     public MapToXmlRowHandler(String wrapper, String item) {
>         this.wrapper = wrapper;
>         this.item = item;
>         builder = new StringBuilder();
>     }
>
>     public void handleRow(Object object) {
>         Map<String, Object> m = (Map<String, Object>) object;
>
>         builder.append("<").append(item).append(">");
>
>         for(String key : m.keySet()){
>             // todo: make sure these values are xml friendly
>             builder.append("<").append(key).append(">")
>                    .append(m.get(key))
>                    .append("</").append(key).append(">");
>         }
>
>         builder.append("</").append(item).append(">");
>     }
>
>     public String getResultsAsXml(){
>         StringBuilder result = new StringBuilder("<" + wrapper + ">")
>                 .append(builder)
>                 .append("</").append(wrapper).append(">");
>         return result.toString();
>     }
>
> }
> ===
>
> Larry
>
>
> On 3/15/07, netsql <ce...@gmail.com> wrote:
> > In the past I allways returned arraylist of hashmap and that works
> EXCLENT.
> >
> > I now want to return xml. I saw some notes on xstream and it works but
> not
> > so well, I do not like the xml it makes.
> >
> > Is there snipets or more ideas to help me?
> >
> > tia,
> > .V
> >
>

Re: ibatis to return xml instead of arraylist (of hashmap).

Posted by Larry Meadors <lm...@apache.org>.
Vic,

You could write a row handler to take the returned maps and a string
builder to create xml.

One class would handle all of your requirements.

Something like this is close - testing is an exercise left to the reader. ;-)

===
public class MapToXmlRowHandler
        implements RowHandler
{
    private String wrapper;
    private String item;
    private StringBuilder builder;


    public MapToXmlRowHandler(String wrapper, String item) {
        this.wrapper = wrapper;
        this.item = item;
        builder = new StringBuilder();
    }

    public void handleRow(Object object) {
        Map<String, Object> m = (Map<String, Object>) object;

        builder.append("<").append(item).append(">");

        for(String key : m.keySet()){
            // todo: make sure these values are xml friendly
            builder.append("<").append(key).append(">")
                   .append(m.get(key))
                   .append("</").append(key).append(">");
        }

        builder.append("</").append(item).append(">");
    }

    public String getResultsAsXml(){
        StringBuilder result = new StringBuilder("<" + wrapper + ">")
                .append(builder)
                .append("</").append(wrapper).append(">");
        return result.toString();
    }

}
===

Larry


On 3/15/07, netsql <ce...@gmail.com> wrote:
> In the past I allways returned arraylist of hashmap and that works EXCLENT.
>
> I now want to return xml. I saw some notes on xstream and it works but not
> so well, I do not like the xml it makes.
>
> Is there snipets or more ideas to help me?
>
> tia,
> .V
>

Re: ibatis to return xml instead of arraylist (of hashmap).

Posted by Chris Lamey <cl...@localmatters.com>.
xstream is pretty flexible, take a look at the Converter examples to see
how you can tweak the output.  If what you want is vastly different than
your object model, you can either create a adapter object model that
takes data from iBATIS or use some kind of xml transform.

We use stx, which is a small project, but the app is really fast and
light on memory, for streaming xml transforms.  When you've got gigs of
xml, any kind of in-memory tree transform (xslt) won't do.

Cheers,
topher

On Thu, 2007-03-15 at 19:36 -0700, netsql wrote:
> In the past I allways returned arraylist of hashmap and that works
> EXCLENT.
> 
> I now want to return xml. I saw some notes on xstream and it works but
> not so well, I do not like the xml it makes.
> 
> Is there snipets or more ideas to help me? 
> 
> tia,
> .V