解析xml数据



  最近看了一下以前学android没有看懂的视频,在学习了javweb后总算是有点体会了。


      demo1

      服务端动态生成xml格式的数据后,客服端解析数据并且显示
   
      1、打开eclipse,
 新建web工程


      建一个servlet,获取到java代码里面的数据,存入到request对象

      建立一个 jsp,显示xml格式的数据

      建立一个pojo,建立实体类

      在dao包下,建立一个接口,得到对象里的数据

      在daoimpl包下,建立一个类,实现这个接口


     2、详细代码

package com.lin.pojo;

public class News {

	private Integer id;
	
	private String title;
	
	private String timelength;
	
	public News(){}
	
	public News(Integer id, String title, String timelength) {
		super();
		this.id = id;
		this.title = title;
		this.timelength = timelength;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getTimelength() {
		return timelength;
	}
	public void setTimelength(String timelength) {
		this.timelength = timelength;
	}
	
}

   接口:

package com.lin.service.impl;

import java.util.List;

import com.lin.pojo.News;

public interface VideoNewsService {

	List<News> getLastNews();
	
}

实现接口的类

package com.lin.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.lin.pojo.News;

public class VideoNewsServiceBean implements VideoNewsService {

	public List<News> getLastNews(){
		List<News> newses=new ArrayList<News>();
         newses.add(new News(12,"大会会的流量","49"));
         newses.add(new News(13,"大会会的苦难","39"));
         newses.add(new News(15,"睡觉的浏览","95"));
         return newses;
	}
	
	 
	
}

Servlet代码

package com.lin.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lin.pojo.News;
import com.lin.service.impl.VideoNewsService;
import com.lin.service.impl.VideoNewsServiceBean;

 
public class ListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
 
	private VideoNewsService service=new VideoNewsServiceBean();
	
    public ListServlet() {
        // TODO Auto-generated constructor stub
    }
 
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	 
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 List<News> videos=service.getLastNews();
		  
			 request.setAttribute("videos", videos);
		     request.getRequestDispatcher("/WEB-INF/page/videoNews.jsp").forward(request, response);
		 	    
	}

}

jsp代码

<%@ page language="java" contentType="text/xml; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><?xml version="1.0" encoding="utf-8"?>
<videonews><c:forEach items="${videos}" var="video">
<news id="${video.id}">
<title>${video.title}</title>
<timelength>${video.timelength}</timelength>
</news></c:forEach>
</videonews>

   注意:contentType为xml格式。引入了jstl标签,需要在lib目录下添加jar文件jstl-1.2.jar,standard-1.1.2.jar

web.xml的映射

<servlet>
    <description></description>
    <display-name>ListServlet</display-name>
    <servlet-name>ListServlet</servlet-name>
    <servlet-class>com.lin.servlet.ListServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ListServlet</servlet-name>
    <url-pattern>/ListServlet</url-pattern>
  </servlet-mapping>
  <servlet>

3、运行

     在浏览器里输入http://localhost:8080/videonews/ListServlet

   显示出
解析xml数据

4、总结过程。servlet的内置对象request里存储了videos集合,jsp里迭代了

这个结合的元素

5、完成了服务端的开发后,接下来就是客服端了,写的非常简单。

          (1)、新建android工程

            listView.setAdapter(simpleadapter);列表显示

           关键是对xml的解析

      utils包里面新建新建一个java类

          

public static List<News> getLastNews() {
		
//localhost改为本机ip地址
//涉及网络访问,添加权限,开辟线程
//第一个方法得到流,
		String path="http://192.168.189.1:8080/videonews/ListServlet";
		try{
			URL url=new URL(path);
			HttpURLConnection conn=(HttpURLConnection) url.openConnection();
			conn.setConnectTimeout(5000);
			conn.setRequestMethod("GET");
			if(conn.getResponseCode()==200){
				InputStream inStream=conn.getInputStream();
				return parseXML(inStream);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}

	private static List<News> parseXML(InputStream inStream)throws Exception {
 		
//解析流,解析xml,getAttribute(),getName(),nextText()
                List<News> newses=new ArrayList<News>();
		News news=null;
		XmlPullParser parser=Xml.newPullParser();
 		parser.setInput(inStream,"UTF-8");
 		int event=parser.getEventType();
 		while(event!=XmlPullParser.END_DOCUMENT){
 			
 			switch (event) {
			case XmlPullParser.START_TAG:
				if("news".equals(parser.getName())){
					int id=new Integer(parser.getAttributeValue(0));
				     news=new News();
				     news.setId(id);
				}else if("title".equals(parser.getName())){
					news.setTitle(parser.nextText());//文本
				}else if("timelength".equals(parser.getName())){
					news.setTimelength(parser.nextText());
				}
					break;

				case XmlPullParser.END_TAG:
					if("news".equals(parser.getName())){
						newses.add(news);
					}
				break;
			}
 			event=parser.next();
 		}
		return newses;
	}

 

 

             解析xml数据

相关推荐