Chinaunix首页 | 论坛 | 博客
  • 博客访问: 209744
  • 博文数量: 102
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1015
  • 用 户 组: 普通用户
  • 注册时间: 2013-06-05 16:45
文章存档

2014年(73)

2013年(29)

我的朋友

分类: Android平台

2014-04-10 18:59:36

01_20_XML文件解析







点击(此处)折叠或打开

  1. //XMLActitity.java
  2. package mars.xml;

  3. import java.io.StringReader;

  4. import javax.xml.parsers.SAXParserFactory;

  5. import mars.utils.HttpDownloader;

  6. import org.xml.sax.InputSource;
  7. import org.xml.sax.XMLReader;

  8. import android.app.Activity;
  9. import android.os.Bundle;
  10. import android.view.View;
  11. import android.view.View.OnClickListener;
  12. import android.widget.Button;

  13. public class XMLActitity extends Activity {
  14.     /** Called when the activity is first created. */
  15.     private Button parseButton ;
  16.     @Override
  17.     public void onCreate(Bundle savedInstanceState) {
  18.         super.onCreate(savedInstanceState);
  19.         setContentView(R.layout.main);
  20.         parseButton = (Button)findViewById(R.id.parseButton);
  21.         parseButton.setOnClickListener(new ParseButtonListener());
  22.     }
  23.     
  24.     class ParseButtonListener implements OnClickListener{
  25.         @Override
  26.         public void onClick(View v) {
  27.             HttpDownloader hd = new HttpDownloader();
  28.             String resultStr = hd.download("");
  29.             System.out.println(resultStr);

  30.             
  31.             /*1、创建一个SAXParserFactory:factoryy = SAXParserFactory.newInstance()
  32.              *2、创建一个XMLReader:reader = factory.newSAXParser().getXMLReader()
  33.              *3、为XMLReader设置内容处理器:reader.setContentHandler(new MyContentHandler());
  34.              *4、开始解析文件:reader.parse(new InputSource(new StringReader(resultStr)))
  35.              *        String ->StringReader ->InputSource ->parse*/
  36.             
  37.             try{
  38.                 //创建一个SAXParserFactory
  39.                 SAXParserFactory factory = SAXParserFactory.newInstance();
  40.                 XMLReader reader = factory.newSAXParser().getXMLReader();
  41.                 //为XMLReader设置内容处理器
  42.                 reader.setContentHandler(new MyContentHandler());
  43.                 //开始解析文件
  44.                 reader.parse(new InputSource(new StringReader(resultStr)));
  45.             }
  46.             catch(Exception e){
  47.                 e.printStackTrace();
  48.             }
  49.         }
  50.     }
  51. }

点击(此处)折叠或打开

  1. //MyContentHandler.java
  2. package mars.xml;

  3. import org.xml.sax.Attributes;
  4. import org.xml.sax.SAXException;
  5. import org.xml.sax.helpers.DefaultHandler;

  6. public class MyContentHandler extends DefaultHandler {
  7.     String hisname, address, money, sex, status;
  8.     String tagName;

  9.     public void startDocument() throws SAXException {
  10.         System.out.println("````````begin````````");
  11.     }

  12.     public void endDocument() throws SAXException {
  13.         System.out.println("````````end````````");
  14.     }

  15.     /*startElement(String namespaceURI, String localName,
  16.             String qName, Attributes attr)            
  17.      * namespaceURI:表命名空间
  18.      * localName:没有前缀的标签
  19.      * qName:表带前缀的标签
  20.      * attr:表标签的属性*/
  21.     public void startElement(String namespaceURI, String localName,
  22.             String qName, Attributes attr) throws SAXException {
  23.         tagName = localName;
  24.         if (localName.equals("worker")) {
  25.             //获取标签的全部属性
  26.             for (int i = 0; i < attr.getLength(); i++) {
  27.                 System.out.println(attr.getLocalName(i) + "=" + attr.getValue(i));
  28.             }
  29.         }
  30.     }

  31.     public void endElement(String namespaceURI, String localName, String qName)
  32.             throws SAXException {
  33.         //在workr标签解析完之后,会打印出所有得到的数据
  34.         tagName = "";
  35.         if (localName.equals("worker")) {
  36.             this.printout();
  37.         }
  38.     }
  39.     
  40.     /*characters(char[] ch, int start, int length)得到标签的内容
  41.      * ch[] 存放读到的数据放在ch数组
  42.      * start:表从哪开始是SAX读取的内容、
  43.      * length表长度*/
  44.     public void characters(char[] ch, int start, int length)
  45.             throws SAXException {
  46.         if (tagName.equals("name"))
  47.             hisname = new String(ch, start, length);
  48.         else if (tagName.equals("sex"))
  49.             sex = new String(ch, start, length);
  50.         else if (tagName.equals("status"))
  51.             status = new String(ch, start, length);
  52.         else if (tagName.equals("address"))
  53.             address = new String(ch, start, length);
  54.         else if (tagName.equals("money"))
  55.             money = new String(ch, start, length);
  56.     }

  57.     private void printout() {
  58.         System.out.print("name: ");
  59.         System.out.println(hisname);
  60.         System.out.print("sex: ");
  61.         System.out.println(sex);
  62.         System.out.print("status: ");
  63.         System.out.println(status);
  64.         System.out.print("address: ");
  65.         System.out.println(address);
  66.         System.out.print("money: ");
  67.         System.out.println(money);
  68.         System.out.println();
  69.     }

  70. }


点击(此处)折叠或打开

  1. //HttpDownloader.java
  2. package mars.utils;

  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.InputStreamReader;
  8. import java.net.HttpURLConnection;
  9. import java.net.MalformedURLException;
  10. import java.net.URL;


  11. public class HttpDownloader {
  12.     private URL url = null;

  13.     /**
  14.      * 根据URL下载文件,前提是这个文件当中的内容是文本,函数的返回值就是文件当中的内容
  15.      * 1.创建一个URL对象
  16.      * 2.通过URL对象,创建一个HttpURLConnection对象
  17.      * 3.得到InputStram
  18.      * 4.从InputStream当中读取数据
  19.      * @param urlStr
  20.      * @return
  21.      */
  22.     public String download(String urlStr) {
  23.         StringBuffer sb = new StringBuffer();
  24.         String line = null;
  25.         BufferedReader buffer = null;
  26.         try {
  27.             // 创建一个URL对象
  28.             url = new URL(urlStr);
  29.             // 创建一个Http连接
  30.             HttpURLConnection urlConn = (HttpURLConnection) url
  31.                     .openConnection();
  32.             // 使用IO流读取数据
  33.             buffer = new BufferedReader(new InputStreamReader(urlConn
  34.                     .getInputStream()));
  35.             while ((line = buffer.readLine()) != null) {
  36.                 sb.append(line);
  37.             }
  38.         } catch (Exception e) {
  39.             e.printStackTrace();
  40.         } finally {
  41.             try {
  42.                 buffer.close();
  43.             } catch (Exception e) {
  44.                 e.printStackTrace();
  45.             }
  46.         }
  47.         return sb.toString();
  48.     }

  49.     /**
  50.      * 该函数返回整形 -1:代表下载文件出错 0:代表下载文件成功 1:代表文件已经存在
  51.      */
  52.     public int downFile(String urlStr, String path, String fileName) {
  53.         InputStream inputStream = null;
  54.         try {
  55.             FileUtils fileUtils = new FileUtils();
  56.             
  57.             if (fileUtils.isFileExist(path + fileName)) {
  58.                 return 1;
  59.             } else {
  60.                 inputStream = getInputStreamFromUrl(urlStr);
  61.                 File resultFile = fileUtils.write2SDFromInput(path,fileName, inputStream);
  62.                 if (resultFile == null) {
  63.                     return -1;
  64.                 }
  65.             }
  66.         } catch (Exception e) {
  67.             e.printStackTrace();
  68.             return -1;
  69.         } finally {
  70.             try {
  71.                 inputStream.close();
  72.             } catch (Exception e) {
  73.                 e.printStackTrace();
  74.             }
  75.         }
  76.         return 0;
  77.     }

  78.     /**
  79.      * 根据URL得到输入流
  80.      *
  81.      * @param urlStr
  82.      * @return
  83.      * @throws MalformedURLException
  84.      * @throws IOException
  85.      */
  86.     public InputStream getInputStreamFromUrl(String urlStr)
  87.             throws MalformedURLException, IOException {
  88.         url = new URL(urlStr);
  89.         HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
  90.         InputStream inputStream = urlConn.getInputStream();
  91.         return inputStream;
  92.     }
  93. }
结果如下:但没有打印 ````````end````````的??????
04-10 11:07:37.103: I/System.out(835): Mark male manager
Guangdong
8000
Merry female employee
Shanghai
4000
Tom male Manger
Beijing
1000000

04-10 11:07:42.133: I/System.out(835): ````````begin````````
04-10 11:07:46.253: I/System.out(835): id=AQ01
04-10 11:07:46.947: I/System.out(835): name: Mark
04-10 11:07:46.947: I/System.out(835): sex: male
04-10 11:07:46.952: I/System.out(835): status: manager
04-10 11:07:46.976: I/System.out(835): address: Guangdong
04-10 11:07:46.981: I/System.out(835): money: 8000
04-10 11:07:47.002: I/System.out(835): id=AQ02
04-10 11:07:47.002: I/System.out(835): name: Merry
04-10 11:07:47.002: I/System.out(835): sex: female
04-10 11:07:47.025: I/System.out(835): status: employee
04-10 11:07:47.025: I/System.out(835): address: Shanghai
04-10 11:07:47.045: I/System.out(835): money: 4000
04-10 11:07:47.045: I/System.out(835): id=AQ03
04-10 11:07:47.106: I/System.out(835): name: Tom
04-10 11:07:47.106: I/System.out(835): sex: male
04-10 11:07:47.112: I/System.out(835): status: Manger
04-10 11:07:47.149: I/System.out(835): address: Beijing
04-10 11:07:47.149: I/System.out(835): money: 1000000



阅读(668) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~