首页 > 代码库 > android开发 服务器端访问MySQL数据库,并把数据库中的某张表解析成xml格式输出到浏览器

android开发 服务器端访问MySQL数据库,并把数据库中的某张表解析成xml格式输出到浏览器

我们此时只要写一个Servlet就可以了:

 

 

public class UpdateMenuServlet extends HttpServlet {

    /**

     *

     */

    private static final long serialVersionUID = 1L;

    // 构造方法

    public UpdateMenuServlet() {

        super();

    }

    // 销毁方法

    public void destroy() {

        super.destroy();

    }

    // 响应Get请求

    public void doGet(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        response.setContentType("text/xml");

        PrintWriter out = response.getWriter();

        // 实例化dao

        UpdateDao dao = new UpdateDaoImpl();

        // 获得菜谱列表

        List<Menu> list = dao.getMenuList();

       

        // 拼XML格式数据

        out.println("<?xml version=‘1.0‘ encoding=‘UTF-8‘?>");

        // 根节点

        out.println("<menulist>");

            for (int i = 0; i <list.size(); i++) {

                Menu m = list.get(i);

                out.println("<menu>");

                    // 菜谱编号

                    out.print("<id>");

                        out.print(m.getId());

                    out.println("</id>");

                    // 分类

                    out.print("<typeId>");

                        out.print(m.getTypeId());

                    out.println("</typeId>");

                    // 名称

                    out.print("<name>");

                        out.print(m.getName());

                    out.println("</name>");

                    // 图片路径

                    out.print("<pic>");

                        out.print(m.getPic());

                    out.println("</pic>");

                    // 价格

                    out.print("<price>");

                        out.print(m.getPrice());

                    out.println("</price>");

                    // 备注

                    out.print("<remark>");

                        out.print(m.getRemark());

                    out.println("</remark>");

                   

                out.println("</menu>");

            }

        out.println("</menulist>");

        out.flush();

        out.close();

    }

    // 响应Post请求

    public void doPost(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        doGet(request,response);

    }

    // 初始化方法

    public void init() throws ServletException {

    }

}

然后就是直接在浏览器中输入这个Servlet的具体网址就可以了

<?xml version="1.0" encoding="UTF-8" ?>

- <menulist>

- <menu>

  <id>1</id>

  <typeId>1</typeId>

  <name>水煮鱼</name>

  <pic>test</pic>

  <price>20</price>

  <remark>test</remark>

  </menu>

- <menu>

  <id>2</id>

  <typeId>2</typeId>

  <name>凉拌西红柿</name>

  <pic>tets</pic>

  <price>10</price>

  <remark>test</remark>

  </menu>

- <menu>

  <id>3</id>

  <typeId>3</typeId>

  <name>tofu</name>

  <pic>test</pic>

  <price>8</price>

  <remark>test</remark>

  </menu>

  </menulist>

 

android开发 服务器端访问MySQL数据库,并把数据库中的某张表解析成xml格式输出到浏览器