android xml文件的解析

有问题请加: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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值