Hi !
We're developing app using restlet framework and we have a task to return XML 
data to client when certain resource is accessed. Currently we're doing 
following 

public class MyResource extends Resource {
  
    public MyResource(Context context, Request request, Response response) {
        super(context, request, response);
        getVariants().add(new Variant(MediaType.TEXT_XML));
    }


    public Representation getRepresentation(Variant variant) {
        Representation result = null;

        Collection<MyBean> beans = new MyBeanManager().getBeans();

        StringWriter sw = new StringWriter();
        sw.write("<root>\n");

        try {
            Marshaller m = 
JAXBContext.newInstance(MyBean.class).createMarshaller();
            m.setProperty(Marshaller.JAXB_FRAGMENT, true);
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

            for (MyBean bean : beans) {
                m.marshal(bean, sw);
            }

        } catch (JAXBException e) {
            e.printStackTrace(System.err);
        }
        sw.write("\n</root>\n");

        return new StringRepresentation(sw.toString(), MediaType.TEXT_XML, 
null, CharacterSet.UTF_8);

    }
}


Is it a best practice to put XML via JAXB to string and return it as 
StringRepresentation. Or maybe there're more convenient way to do such task.
Thnx

Reply via email to