Java servlet将数据转换为xml

时间:2016-12-19 18:58:58

标签: java xml servlets

班级城市有变量

private String name;
private int timeZone;
private int pop;

在主servlet类

@Override
  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
      throws ServletException, IOException {
    response.setHeader("Cache-Control", "no-cache");
    response.setHeader("Pragma", "no-cache");
    String cityType = request.getParameter("cityType");
    List<City> cities = CityUtils.findCities(cityType);
    request.setAttribute("cities", cities);
    String format = request.getParameter("format");
    String outputPage;
    if ("xml".equals(format)) {
      response.setContentType("text/xml");
      outputPage = "/WEB-INF/results/cities-xml.jsp";
    } else if ("json".equals(format)) {
      response.setContentType("application/json");
      outputPage = "/WEB-INF/results/cities-json.jsp";
    } else {
      response.setContentType("text/plain");
      outputPage = "/WEB-INF/results/cities-string.jsp";
    }
    RequestDispatcher dispatcher =
      request.getRequestDispatcher(outputPage);
    dispatcher.include(request, response);
  }

对于xml,使用了一个cities-xml

<?xml version="1.0" encoding="UTF-8"?>
<cities>
  <headings>
    <heading>City</heading>
    <heading>Time</heading>
    <heading>Population</heading>
  </headings>
  <city>
    <name>${cities[0].name}</name>
    <time>${cities[0].time}</time>
    <population>${cities[0].population}</population>
  </city>
  <city>
    <name>${cities[1].name}</name>
    <time>${cities[1].time}</time>
    <population>${cities[1].population}</population>
  </city>
  <city>
    <name>${cities[2].name}</name>
    <time>${cities[2].time}</time>
    <population>${cities[2].population}</population>
  </city>
  <city>
    <name>${cities[3].name}</name>
    <time>${cities[3].time}</time>
    <population>${cities[3].population}</population>
  </city>
  <city>
    <name>${cities[4].name}</name>
    <time>${cities[4].time}</time>
    <population>${cities[4].population}</population>
  </city>
</cities>

我的问题是为什么城市人口与城市阶级变量不同。 名称,时间,人口与城市类别中的人口不同

0 个答案:

没有答案