歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

android開發之讀取xml文件

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();
                } 

Copyright © Linux教程網 All Rights Reserved