android xml文件的解析

来源:互联网 发布:java接口压力测试工具 编辑:程序博客网 时间:2024/05/17 06:42
有问题请加:Q群: 241359063  共同走向创业学习之旅。
原创:kylin_zeng  http://blog.chinaunix.net/uid/23795897.html
在此感谢mars 老师的帮助。
转载请注明原创出处,尊重他人的劳动成果。








XMLActivity.java

点击(此处)折叠或打开

  1. package mars.xml;

  2. import java.io.StringReader;

  3. import javax.xml.parsers.SAXParserFactory;

  4. import mars.utils.HttpDownloader;

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

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

  12. public class XMLActitity extends Activity {
  13.     /** Called when the activity is first created. */
  14.     private Button parseButton ;
  15.     @Override
  16.     public void onCreate(Bundle savedInstanceState) {
  17.         super.onCreate(savedInstanceState);
  18.         setContentView(R.layout.main);
  19.         parseButton = (Button)findViewById(R.id.parseButton);
  20.         parseButton.setOnClickListener(new ParseButtonListener());
  21.     }
  22.     
  23.     class ParseButtonListener implements OnClickListener{

  24.         @Override
  25.         public void onClick(View v) {
  26.             HttpDownloader hd = new HttpDownloader();
  27.             String resultStr = hd.download("http://192.168.1.107:8081/voa1500/test.xml");
  28.             System.out.println(resultStr);
  29.             try{
  30.                 //创建一个SAXParserFactory
  31.                 SAXParserFactory factory = SAXParserFactory.newInstance();
  32.                 XMLReader reader = factory.newSAXParser().getXMLReader();
  33.                 //为XMLReader设置内容处理器
  34.                 reader.setContentHandler(new MyContentHandler());
  35.                 //开始解析文件
  36.                 reader.parse(new InputSource(new StringReader(resultStr)));
  37.             }
  38.             catch(Exception e){
  39.                 e.printStackTrace();
  40.             }
  41.         }
  42.         
  43.     }
  44. }
MyContentHandler.java

点击(此处)折叠或打开

  1. package mars.xml;

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

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

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

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

  14.     public void startElement(String namespaceURI, String localName,
  15.             String qName, Attributes attr) throws SAXException {
  16.         tagName = localName;
  17.         if (localName.equals("worker")) {
  18.             //获取标签的全部属性
  19.             for (int i = 0; i < attr.getLength(); i++) {
  20.                 System.out.println(attr.getLocalName(i) + "=" + attr.getValue(i));
  21.             }
  22.         }
  23.     }

  24.     public void endElement(String namespaceURI, String localName, String qName)
  25.             throws SAXException {
  26.         //在workr标签解析完之后,会打印出所有得到的数据
  27.         tagName = "";
  28.         if (localName.equals("worker")) {
  29.             this.printout();
  30.         }
  31.     }
  32.     public void characters(char[] ch, int start, int length)
  33.             throws SAXException {
  34.         if (tagName.equals("name"))
  35.             hisname = new String(ch, start, length);
  36.         else if (tagName.equals("sex"))
  37.             sex = new String(ch, start, length);
  38.         else if (tagName.equals("status"))
  39.             status = new String(ch, start, length);
  40.         else if (tagName.equals("address"))
  41.             address = new String(ch, start, length);
  42.         else if (tagName.equals("money"))
  43.             money = new String(ch, start, length);
  44.     }

  45.     private void printout() {
  46.         System.out.print("name: ");
  47.         System.out.println(hisname);
  48.         System.out.print("sex: ");
  49.         System.out.println(sex);
  50.         System.out.print("status: ");
  51.         System.out.println(status);
  52.         System.out.print("address: ");
  53.         System.out.println(address);
  54.         System.out.print("money: ");
  55.         System.out.println(money);
  56.         System.out.println();
  57.     }

  58. }

mars视频教程ppt和代码01_20_ppt_src.zip


<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(2) | 评论(0) | 转发(0) |
0

上一篇:ContentProvider 用法

下一篇:android wifi 的开启,关闭,查询等。

相关热门文章
  • Android之开发环境搭建
  • Android自定义View的实现...
  • AndroidManifest.xml配置文件...
  • Android源码调试方法详解...
  • 不用vs和cygwin!Eclipse+cdt...
  • 请问Linux默认shell的是什么 ...
  • 谁能够帮我解决LINUX 2.6 10...
  • 现在的博客积分不会更新了吗?...
  • shell怎么读取网页内容...
  • ssh等待连接的超时问题...
给主人留下些什么吧!~~