Another option would be to convert your List to an XML Document 
or Node object in your action.

public org.w3c.dom.Node list() {
    // create XML books Document
    // iterate through list adding book Nodes to it

    return booksNode;
}



>From reading the javadoc in the org.apache.struts2.views.xslt package,
I believe yet another option would be to create a custom AdapterNode 
class for your Book object and register it with the XSLT AdapterFactory.  If 
I read it correctly, Struts2 would use your BookAdapterNode class to render 
the XML.  If you named your method, getBooks, this may give you the xml that 
you 
are looking for.


--
John


> -----Original Message-----
> From: Jorge Martín Cuervo [mailto:[EMAIL PROTECTED] 
> Sent: Friday, June 29, 2007 3:31 AM
> To: Struts Users Mailing List
> Subject: Re: How to convert a list of object to XML
> 
> you have to overwrite the toString method in class Book, and then
> iterate over the List to complete the xml. somthing like this:
> 
> [...]
> public String toString() {
>       return "<book><id>" + id + "</id><name>" + name + 
> "</name></book>";
> }
> [...]
> 
> public String list() {
> 
>       StringBuffer sb = new StringBuffer("<books>");
>       for(Iterator iter = 
> bookDao.getList().iterator;iter.hasNext();) {
>               sb.appengd(iter.next(());
> }
>       sb.append("</books>");
> 
>       return sb.toString();
> }
> 
> [...]
> 
> 
> sorry if there is any syntax error, i've coded from scratch 
> in my email
> client.
> 
> best regards, Jorge.
> 
> 
> El vie, 29-06-2007 a las 14:50 +0800, chenshibing escribió:
> > Utilizing Struts2 xslt result type, how to convert a list 
> of object to XML file?
> > 
> >  
> > 
> > Action:
> > 
> >  
> > 
> > public class BookAction extends ActionSupport {
> > 
> >     private List<Book> bookList;
> > 
> >     
> > 
> >     public String list() throws Exception {
> > 
> >        bookList = bookDao.getList();
> > 
> >        
> > 
> >        return SUCCESS;
> > 
> >     }
> > 
> > }
> > 
> >  
> > 
> > Model:
> > 
> >  
> > 
> > public class Book {
> > 
> >     private Long id;
> > 
> >     private String name;
> > 
> >  
> > 
> >     // getter and setter.
> > 
> > }
> > 
> >  
> > 
> > I want to convert book list to a XML file as the following:
> > 
> >  
> > 
> > <?xml version="1.0" encodeing="UTF-8"?>
> > 
> > <books>
> > 
> >     <book>
> > 
> >        <id>1</id>
> > 
> >        <name>Thinking in Java</name>
> > 
> >     </book>
> > 
> >     <book>
> > 
> >        <id>2</id>
> > 
> >        <name>Effective Java</name>
> > 
> >     </book>
> > 
> >     ...
> > 
> > </books>
> > 
> >  
> > 
> > I'm new to XSLT, how to write an xsl template to get this job done?
> > 
> > Any help will be appreciated.
> > 
> >  
> > 
> > Real Chen
> > 
> -- 
> ____________________________________
> Jorge Martin Cuervo
> Analista Programador
>  
> Outsourcing Emarketplace
> deFacto Powered by Standards
>  
> email <[EMAIL PROTECTED]>
> voz +34 985 129 820
> voz +34 660 026 384
> ____________________________________
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to