|
一旦所有的 JavaBean 都实现了 toXml() 方法,那么把任意对象图序列化成 XML 文档并返回给 Ajax 客户机就简单了,如清单 2 所示。
[php]
清单 2. 从 JDOM 元素生成 XML 响应
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws java.io.IOException, ServletException {
String custId = req.getParameter("username" ;
Customer customer = getCustomer(custId);
Element responseElem = customer.toXml();
Document responseDoc = new Document(responseElem);
res.setContentType("application/xml" ;
new XMLOutputter().output(responseDoc,res.getWriter());
}
............
[/php] |
|