You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by chenshibing <ch...@lifeisgreat.com.cn> on 2007/06/29 08:50:25 UTC
How to convert a list of object to XML
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
Re: How to convert a list of object to XML
Posted by Matthias Fischer <mf...@gmx.de>.
Hi,
> >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.
>
Does anyone of you understand how I can register the custom
AdapterFactory? The Javadoc says I have to call the register method in
AdapterFactory, but where in my code do I do this... where is it
instantiated? Any ideas?
Matthias
--
-------------------------------------------------------------------
Matthias Fischer Fon: +49-7541-6047-148
doubleSlash Net-Business GmbH Fax: +49-7541-6047-111
Müllerstr. 12 B matthias.fischer@doubleSlash.de
D-88045 Friedrichshafen http://doubleSlash.de
-------------------------------------------------------------------
doubleSlash Net-Business GmbH
Geschäftsführung: Konrad Krafft, Oliver Belikan, Jan Schubert
Sitz, Registergericht: Friedrichshafen, Amtsgericht Ulm HRB 631718
-------------------------------------------------------------------
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
RE: How to convert a list of object to XML
Posted by John Krueger <jk...@pathfire.com>.
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:jorge.martin@defactops.com]
> 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 <jo...@defactops.com>
> voz +34 985 129 820
> voz +34 660 026 384
> ____________________________________
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: How to convert a list of object to XML
Posted by Li <am...@gmail.com>.
you can use xstream there are quite a few tools out there can help you
achieve object->XML.
The xstream site is here http://xstream.codehaus.org/
On 6/29/07, Jorge Martín Cuervo <jo...@defactops.com> wrote:
>
> 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 <jo...@defactops.com>
> voz +34 985 129 820
> voz +34 660 026 384
> ____________________________________
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
--
Small win by playing smart
Big win by playing honest
Re: How to convert a list of object to XML
Posted by Jorge Martín Cuervo <jo...@defactops.com>.
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 <jo...@defactops.com>
voz +34 985 129 820
voz +34 660 026 384
____________________________________
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org