publicActionForwardshow(ActionMappingmapping,ActionFormform, HttpServletRequestrequest,HttpServletResponseresponse)throwsIOException{ request.setCharacterEncoding("utf-8"); response.setContentType("text/xml;charset=UTF-8"); Stringparent=request.getParameter("parent"); intpid=0; if(parent==null||parent.trim().length()<1){ returnnull; } pid=Integer.parseInt(parent); Listlist=dao.findByProperty("parent",pid); StringxmlString=createXML(list);// System.out.println("xml:"+xmlString); PrintWriterout=response.getWriter(); out.print(xmlString); out.flush(); returnnull; } privateStringcreateXML(Listlist){ Documentdocument=DocumentHelper.createDocument(); Elementroot=document.addElement("DEPARTMENTS"); Iteratorit=list.iterator(); while(it.hasNext()){ Departmentdept=it.next(); Elementedept=root.addElement("DEPARTMENT"); edept.addElement("ID").addText(dept.getId()+""); edept.addElement("DEPTNAME").addText(dept.getDeptName()); edept.addElement("PARENT").addText(dept.getParent()+""); } StringxmlString=document.asXML(); returnxmlString; }
这里是我通过数据库内容创建xml并返回到前台的代码,你参考下!