问题描述
- 怎么在后天获取xml的数据,并返回到前台页面
- 前台是一个导航菜单,需要获取xml中的名称属性,并放置在li里边,求大神解救
解决方案
PrintWriter writer = null; try{ response.setContentType(""text/xml;charset=UTF-8""); writer = response.getWriter(); writer.write(result); writer.flush(); }catch(Exception e){ e.printStackTrace(); }
解决方案二:
public class XmlTOHServlet extends HttpServlet { public void doPost(HttpServletRequest request HttpServletResponse response) throws ServletException IOException { request.setCharacterEncoding(""UTF-8""); response.setContentType(""text/html; charset=UTF-8""); StringBuffer bxml = new StringBuffer(); String line = null; //从获得请求中的数据流 try { BufferedReader reader = request.getReader(); while((line = reader.readLine()) != null) { bxml.append(line); } } catch(Exception e) { System.out.println(e.toString()); } String xml =bxml.toString(); Document xmlDoc = null; try { xmlDoc = DocumentBuilderFactory.newInstance() .newDocumentBuilder() .parse(new ByteArrayInputStream(xml.getBytes())); } catch(ParserConfigurationException e) { System.out.println(e.getStackTrace()); } catch(SAXException e) { System.out.println( e); } String uname = xmlDoc.getElementsByTagName(""name"").item(0).getFirstChild().getNodeValue(); String city = xmlDoc.getElementsByTagName(""ad"").item(0).getFirstChild().getNodeValue(); String street = xmlDoc.getElementsByTagName(""yb"").item(0).getFirstChild().getNodeValue(); String responseText = ""您发送给""+uname+""地址是""+city+""邮编是""+street+""邮件,还需要一天到达目的地!""; response.setContentType(""text/xml; charset=UTF-8""); response.setHeader(""Cache-Control""no-cache""); PrintWriter out = response.getWriter(); out.println(responseText); out.close(); } }
解决方案三:
访问页面返回XML格式的数据
访问页面返回XML格式的数据
时间: 2024-10-30 03:55:25