你好,游客 登录 注册 搜索
背景:
阅读新闻

android开发之读取xml文件

[日期:2011-10-17] 来源:Linux社区  作者:blackproof [字体: ]

Android读取xml文件:

1.SAX

1.生成xml读取器SAXParser

2.创建自己的内容处理器,处理xml节点、属性等等,需要继承于 DefaultHandler 并且has a自己的数据结构

3.读取器1加载处理器2,读取xml

               /* 创建xml文件的URL*/
               URL url = new URL("http://www.linuxidc.com/example.xml");

               /* 从SAXParserFactory获取SAXParser. */
               SAXParserFactory spf = SAXParserFactory.newInstance();
               SAXParser sp = spf.newSAXParser();

               /* 从SAXParser获取XMLReader. */
               XMLReader xr = sp.getXMLReader();
               /* 创建我们自己的内容处理器*/
               ExampleHandler myExampleHandler = new ExampleHandler();
               /* 用内容处理器处理XMLReader*/
               xr.setContentHandler(myExampleHandler);
              
               /* XMLReader获取XML文件 */
               xr.parse(new InputSource(url.openStream()));
              

               /* 用我们的ExampleHandler解析XML中的数据. */
               ParsedExampleDataSet parsedExampleDataSet =
                  myExampleHandler.getParsedData();

自己的处理器的数据结构:
     private ParsedExampleDataSet myParsedExampleDataSet =
                  new ParsedExampleDataSet();
 
     public ParsedExampleDataSet getParsedData() {
          return this.myParsedExampleDataSet;
     }
     @Override
     public void startDocument() throws SAXException {
          this.myParsedExampleDataSet = new ParsedExampleDataSet();
          Log.v("ParsingXML", "startDocument()");
     }
     @Override
     public void endDocument() throws SAXException {
          Log.v("ParsingXML", "endDocument()");
     }
     /** 当遇到开始标签时被调用,比如:
      * <tag>
      * 可以得到标签属性值,比如:
      * <tag attribute="attributeValue">*/
     @Override
     public void startElement(String namespaceURI, String localName,
               String qName, Attributes atts) throws SAXException {
          if (localName.equals("outertag")) {
               this.in_outertag = true;
          }else if (localName.equals("innertag")) {
               this.in_innertag = true;
          }else if (localName.equals("mytag")) {
               this.in_mytag = true;
          }else if (localName.equals("tagwithnumber")) {
               // 提取标签属性值
               String attrValue = atts.getValue("thenumber");
               int i = Integer.parseInt(attrValue);
               myParsedExampleDataSet.setExtractedInt(i);
          }
          Log.v("ParsingXML", "StartElement(): "+ localName);
     }
    
     /** 当遇到终止标签时被调用,比如:
      * </tag> */
     @Override
     public void endElement(String namespaceURI, String localName, String qName)
               throws SAXException {
          if (localName.equals("outertag")) {
               this.in_outertag = false;
          }else if (localName.equals("innertag")) {
               this.in_innertag = false;
          }else if (localName.equals("mytag")) {
               this.in_mytag = false;
          }else if (localName.equals("tagwithnumber")) {
               // Nothing to do here
          }
          Log.v("ParsingXML", "endElement(): "+ localName);
     }
    
     /** 当遇到如下结构时被调用
      * <tag>characters</tag>
      */
     @Override
    public void characters(char ch[], int start, int length) {
          if(this.in_mytag){
          String str = new String(ch, start, length);
          myParsedExampleDataSet.setExtractedString(str);
          Log.v("ParsingXML", "characters(): " + str);
     }
     
    }

 

2.DOM

1.获取xml
2.构造DOM
3.获取xml内容
            /*获取xml*/
            String feed = getString(R.string.feed);
    url = new URL(feed);
        
    URLConnection connection = url.openConnection();
      
    HttpURLConnection httpConnection = (HttpURLConnection)connection;
    int responseCode = httpConnection.getResponseCode();

    if (responseCode == HttpURLConnection.HTTP_OK) {
      InputStream in = httpConnection.getInputStream();
     
              /*构造DOM*/   
      DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
      DocumentBuilder db = dbfactory.newDocumentBuilder();

      // 解析地震feed
      Document dom = db.parse(in);     
      Element docEle = dom.getDocumentElement();
       
      // 清空旧的地震信息
      infoList.clear();
      
              /*获取xml内容*/  
      // 获得地震信息的列表
      NodeList nl = docEle.getElementsByTagName("entry");
      if (nl != null && nl.getLength() > 0) {
        for (int i = 0 ; i < nl.getLength(); i++) {
          Element entry = (Element)nl.item(i);
          Element title = (Element)entry.getElementsByTagName("title").item(0);
          Element geo = (Element)entry.getElementsByTagName("georss:point").item(0);
          Element when = (Element)entry.getElementsByTagName("updated").item(0);

          String details = title.getFirstChild().getNodeValue();
          String point = geo.getFirstChild().getNodeValue();
          String date = when.getFirstChild().getNodeValue();
                } 

linux
相关资讯       Android基础教程  Android开发教程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款