Announcement Announcement Module
Collapse
No announcement yet.
XML output Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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

  • #2
    Some ideas...

    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(
        HttpServletRequest req, HttpServletResponse resp) throws Exception
    {
      resp.setContentType("text/xml");  // 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;
    
    /**
     * <p>Description: converts between domain objects and XML.</p>
     */
    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;
    
    /**
     * <p>Description: implementation of an XmlConverter. Delegates to an XStream
     *   instance. We don't directly use the XStream class throughout our app
     *   because:
     *   <ul>
     *     <li>that would couple us too tightly to this library</li>
     *     <li>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.</li>
     *   </ul>
     */
    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("someAlias", 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 -> XML conversion. I'd be interested to hear how other people go about this task.

    HTH,

    Comment


    • #3
      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 "text/xml";
      	}
      
      	public void render(Map model, HttpServletRequest request,
      			HttpServletResponse response) throws Exception {
      
      		// Retrieve data from model
      		Document doc = (Document) model.get("xml");
      		
      		// Write the XML document to the reponse output stream
      		OutputStream out = response.getOutputStream();
      	    try {
      			Serializer serializer = new Serializer(out, "utf-8");
      			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. (http://www.xom.nu/).

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

      Code:
      	<bean id="beanNameViewResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver">
      		<property name="order"><value>1</value></property> 
      	</bean>
      
      	<bean id="xmlView" class="org.aoide.web.view.XmlView"/>
      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(HttpServletRequest request,
      			HttpServletResponse response) throws Exception {
      		
      		// Set relevant HTTP headers
      		response.setHeader("Content-Disposition", "attachment; filename=\"artist.xml\"");
      		
      		String id = (String) request.getParameter("id");
      		Artist artist = getDao().load(id);
      		Document xomDoc = artist.toXmlDocument(); 
      		
      	    Map<Object, Object> model = new HashMap<Object, Object>();
      	    model.put("xml", xomDoc);
      
      		return new ModelAndView("xmlView", model);
      	}
      	
      }
      Hope this helps,

      Arjan Huijzer

      Comment


      • #4
        Keeping us in suspense!

        Originally posted by huijzer
        My solution ... etc.
        Nice. But the bit in which I'm really interested is artist.toXmlDocument(). How do your domain objects turn themselves into XML?

        Comment


        • #5
          Url mapping

          Hi huijzer

          How did you map the request to the ArtistExportController?

          Could you also include the ArtistExportController bean definition?

          Thanks in advance,

          Colin

          Originally posted by huijzer View Post
          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 "text/xml";
          	}
          
          	public void render(Map model, HttpServletRequest request,
          			HttpServletResponse response) throws Exception {
          
          		// Retrieve data from model
          		Document doc = (Document) model.get("xml");
          		
          		// Write the XML document to the reponse output stream
          		OutputStream out = response.getOutputStream();
          	    try {
          			Serializer serializer = new Serializer(out, "utf-8");
          			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. (http://www.xom.nu/).

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

          Code:
          	<bean id="beanNameViewResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver">
          		<property name="order"><value>1</value></property> 
          	</bean>
          
          	<bean id="xmlView" class="org.aoide.web.view.XmlView"/>
          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(HttpServletRequest request,
          			HttpServletResponse response) throws Exception {
          		
          		// Set relevant HTTP headers
          		response.setHeader("Content-Disposition", "attachment; filename=\"artist.xml\"");
          		
          		String id = (String) request.getParameter("id");
          		Artist artist = getDao().load(id);
          		Document xomDoc = artist.toXmlDocument(); 
          		
          	    Map<Object, Object> model = new HashMap<Object, Object>();
          	    model.put("xml", xomDoc);
          
          		return new ModelAndView("xmlView", model);
          	}
          	
          }
          Hope this helps,

          Arjan Huijzer

          Comment

          Working...
          X