Back Forum Reply New

XML output

Hi all,
    I want send my responce as xml to the client application.How can i acheive this? I saw XMLViewResolver for this.But i got confused with the configuration.Can any one help me out.

Thnx in advance
Jophis

From reading the JavaDoc, XmlViewResolver doesn't generate XML output; it simply loads its view resolution rules from an XML file.

I too have an app that needs to send XML to the client (an OpenLaszlo client, FWIW). My controller, which extends Spring's AbstractController, does something like this:

Code:protected ModelAndView handleRequestInternal(   fromServletRequest req, fromServletResponse resp) throws Exception
{ resp.setContentType(quot;text/xmlquot;);  // actually I use a constant Writer writer = resp.getWriter(); Object objectToConvert = ...;  // get the object however you like xmlConverter.toXML(object, writer); writer.close(); return null;  // indicates this controller did all necessary processing
}
Where the XmlConverter interface looks like this:

Code:
package blah;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;

/*** lt;pgt;Description: converts between domain objects and XML.lt;/pgt;*/
public interface XmlConverter {
Object fromXML(InputStream xml, Object root); Object fromXML(InputStream input); Object fromXML(Reader xml, Object root); Object fromXML(Reader xml); Object fromXML(String xml, Object root); Object fromXML(String xml); void toXML(Object obj, OutputStream out); void toXML(Object obj, Writer out); String toXML(Object obj);
}
And the implementation looks like this:Code:
package blah;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;

import com.thoughtworks.xstream.XStream;

/*** lt;pgt;Description: implementation of an XmlConverter. Delegates to an XStream*   instance. We don't directly use the XStream class throughout our app*   because:*   lt;ulgt;*     lt;ligt;that would couple us too tightly to this librarylt;/ligt;*     lt;ligt;we only want to configure all the XStream aliases once and in the*       domain tier, not in the Spring config files or web-tier classes.lt;/ligt;*   lt;/ulgt;*/
public class XmlConverterImpl implements XmlConverter {
// Properties private XStream xStream;    // the delegate  /**  * Constructor   */ public XmlConverterImpl() {   xStream = new XStream();   // Add aliases for all domain classes   xStream.alias(quot;someAliasquot;, MyDomainClass.class); }
public Object fromXML(InputStream xml, Object root) {   return xStream.fromXML(xml, root); }
public Object fromXML(InputStream input) {   return xStream.fromXML(input); }
public Object fromXML(Reader xml, Object root) {   return xStream.fromXML(xml, root); }
public Object fromXML(Reader xml) {   return xStream.fromXML(xml); }
public Object fromXML(String xml, Object root) {   return xStream.fromXML(xml, root); }
public Object fromXML(String xml) {   return xStream.fromXML(xml); }
public void toXML(Object obj, OutputStream out) {   xStream.toXML(obj, out); }
public void toXML(Object obj, Writer out) {   xStream.toXML(obj, out); }
public String toXML(Object obj) {   return xStream.toXML(obj); }
}
In short, I use XStream behind the scenes to perform the domain -gt; XML conversion. I'd be interested to hear how other people go about this task.

HTH,

My solution to sending XML back to the client was to create create an XmlView class:Code:
public class XmlView implements View {

Log log = LogFactory.getLog(XmlView.class);

public String getContentType() {
return quot;text/xmlquot;;
}

public void render(Map model, fromServletRequest request,
fromServletResponse response) throws Exception {

// Retrieve data from model
Document doc = (Document) model.get(quot;xmlquot;);

// Write the XML document to the reponse output stream
OutputStream out = response.getOutputStream();   try {
Serializer serializer = new Serializer(out, quot;utf-8quot;);
serializer.setIndent(4);
serializer.setMaxLength(64);
serializer.write(doc);
} catch (IOException ex) {
log.error(ex);
}  

}

}
This uses the XOM api for parsing/creating/outputting XML documents. ().

Next, I need to configure this view in my spring config file and define a view reolver for it:Code:

lt;bean id=quot;beanNameViewResolverquot; class=quot;org..web.servlet.view.BeanNameViewResolverquot;gt;
lt;property name=quot;orderquot;gt;lt;valuegt;1lt;/valuegt;lt;/propertygt;
lt;/beangt;

lt;bean id=quot;xmlViewquot; class=quot;org.aoide.web.view.XmlViewquot;/gt;
Finally I can use this view in my controller:Code:
public class ArtistExportController extends AbstractController {

private ArtistDao dao;

public ArtistDao getDao() {
return dao;
}

public void setDao(ArtistDao dao) {
this.dao = dao;
}

protected ModelAndView handleRequestInternal(fromServletRequest request,
fromServletResponse response) throws Exception {

// Set relevant from headers
response.setHeader(quot;Content-Dispositionquot;, quot;attachment; filename=\quot;artist.xml\quot;quot;);

String id = (String) request.getParameter(quot;idquot;);
Artist artist = getDao().load(id);
Document xomDoc = artist.toXmlDocument();
   Maplt;Object, Objectgt; model = new HashMaplt;Object, Objectgt;();   model.put(quot;xmlquot;, xomDoc);

return new ModelAndView(quot;xmlViewquot;, model);
}

}
Hope this helps,

Arjan Huijzer


Originally Posted by huijzerMy solution ... etc.

Nice. But the bit in which I'm really interested is artist.toXmlDocument(). How do your domain objects turn themselves into XML?

Hi huijzer

How did you map the request to the ArtistExportController?

Could you also include the ArtistExportController bean definition?

Thanks in advance,

ColinOriginally Posted by huijzerMy solution to sending XML back to the client was to create create an XmlView class:Code:
public class XmlView implements View {

Log log = LogFactory.getLog(XmlView.class);

public String getContentType() {
return quot;text/xmlquot;;
}

public void render(Map model, fromServletRequest request,
fromServletResponse response) throws Exception {

// Retrieve data from model
Document doc = (Document) model.get(quot;xmlquot;);

// Write the XML document to the reponse output stream
OutputStream out = response.getOutputStream();   try {
Serializer serializer = new Serializer(out, quot;utf-8quot;);
serializer.setIndent(4);
serializer.setMaxLength(64);
serializer.write(doc);
} catch (IOException ex) {
log.error(ex);
}  

}

}
This uses the XOM api for parsing/creating/outputting XML documents. ().

Next, I need to configure this view in my spring config file and define a view reolver for it:Code:

lt;bean id=quot;beanNameViewResolverquot; class=quot;org..web.servlet.view.BeanNameViewResolverquot;gt;
lt;property name=quot;orderquot;gt;lt;valuegt;1lt;/valuegt;lt;/propertygt;
lt;/beangt;

lt;bean id=quot;xmlViewquot; class=quot;org.aoide.web.view.XmlViewquot;/gt;
Finally I can use this view in my controller:Code:
public class ArtistExportController extends AbstractController {

private ArtistDao dao;

public ArtistDao getDao() {
return dao;
}

public void setDao(ArtistDao dao) {
this.dao = dao;
}

protected ModelAndView handleRequestInternal(fromServletRequest request,
fromServletResponse response) throws Exception {

// Set relevant from headers
response.setHeader(quot;Content-Dispositionquot;, quot;attachment; filename=\quot;artist.xml\quot;quot;);

String id = (String) request.getParameter(quot;idquot;);
Artist artist = getDao().load(id);
Document xomDoc = artist.toXmlDocument();
   Maplt;Object, Objectgt; model = new HashMaplt;Object, Objectgt;();   model.put(quot;xmlquot;, xomDoc);

return new ModelAndView(quot;xmlViewquot;, model);
}

}
Hope this helps,

Arjan Huijzer
¥
Back Forum Reply New