Chinaunix首页 | 论坛 | 博客

Go

  • 博客访问: 227576
  • 博文数量: 67
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 783
  • 用 户 组: 普通用户
  • 注册时间: 2013-10-12 16:29
文章分类

全部博文(67)

文章存档

2015年(1)

2014年(47)

2013年(19)

我的朋友

分类: Android平台

2014-10-15 22:40:33

有问题请加: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("");
  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


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