2013-09-25 85 views
0

這就是我想要做的。我寫了一個應該處理我的XML文件的類。用Android中的XMLPullParser解析本地XML文件

package de.lies; 

    import java.io.IOException; 

    import org.xmlpull.v1.XmlPullParser; 
    import org.xmlpull.v1.XmlPullParserException; 

    public class Entry { 
    private static final String ns = null; 

    public final String rank; 
    public final String source; 
    public final String date; 
    public final String headline; 
    public final String description; 
    public final String articleURL; 
    public final String sourceURL; 
    public final String imageURL; 

    private Entry(String rank, String source, String date, String headline, String description, String articleURL, String sourceURL, String imageURL){ 
     this.rank   = rank; 
     this.source   = source; 
     this.date   = date; 
     this.headline  = headline; 
     this.description = description; 
     this.articleURL  = articleURL; 
     this.sourceURL  = sourceURL; 
     this.imageURL  = imageURL; 
    } 

    private Entry readEntry (XmlPullParser parser) throws XmlPullParserException, IOException{ 
     parser.require(XmlPullParser.START_TAG, ns, "entry"); 
     String rank   = null; 
     String source  = null; 
     String date   = null; 
     String headline  = null; 
     String description = null; 
     String articleURL = null; 
     String sourceURL = null; 
     String imageURL  = null; 

     while(parser.next() != XmlPullParser.END_TAG) { 
      if(parser.getEventType() != XmlPullParser.START_TAG){ 
       continue; 
      } 
      String name  = parser.getName(); 

      if(name.equals("rank")){ 
       rank  = readRank(parser); 
      } else if(name.equals("source")){ 
       source  = readSource(parser); 
      } else if(name.equals("date")){ 
       date  = readDate(parser); 
      } else if(name.equals("headline")){ 
       headline = readHeadline(parser); 
      } else if(name.equals("description")){ 
       description = readDescription(parser); 
      } else if(name.equals("articleURL")){ 
       articleURL = readArticleURL(parser); 
      } else if(name.equals("sourceURL")){ 
       sourceURL = readSourceURL(parser); 
      } else if(name.equals("imageURL")){ 
       imageURL = readImageURL(parser); 
      } else { 
       skip(parser); 
      } 
     } 
     return new Entry(rank, source, date, headline, description, articleURL, sourceURL, imageURL); 
    } 

    private String readRank(XmlPullParser parser) throws IOException, XmlPullParserException{ 
     parser.require(XmlPullParser.START_TAG, ns, "rank"); 
     String rank   = readText(parser); 

     parser.require(XmlPullParser.END_TAG, ns, "rank"); 
     return rank; 
    } 

    private String readSource(XmlPullParser parser) throws IOException, XmlPullParserException{ 
     parser.require(XmlPullParser.START_TAG, ns, "source"); 
     String source  = readText(parser); 

     parser.require(XmlPullParser.END_TAG, ns, "source"); 
     return source; 
    } 

    private String readDate(XmlPullParser parser) throws IOException, XmlPullParserException{ 
     parser.require(XmlPullParser.START_TAG, ns, "rank"); 
     String date   = readText(parser); 

     parser.require(XmlPullParser.END_TAG, ns, "rank"); 
     return date; 
    } 

    private String readHeadline(XmlPullParser parser) throws IOException, XmlPullParserException{ 
     parser.require(XmlPullParser.START_TAG, ns, "rank"); 
     String headline  = readText(parser); 

     parser.require(XmlPullParser.END_TAG, ns, "rank"); 
     return headline; 
    } 

    private String readDescription(XmlPullParser parser) throws IOException, XmlPullParserException{ 
     parser.require(XmlPullParser.START_TAG, ns, "rank"); 
     String description  = readText(parser); 

     parser.require(XmlPullParser.END_TAG, ns, "rank"); 
     return description; 
    } 

    private String readArticleURL(XmlPullParser parser) throws IOException, XmlPullParserException{ 
     parser.require(XmlPullParser.START_TAG, ns, "rank"); 
     String articleURL  = readText(parser); 

     parser.require(XmlPullParser.END_TAG, ns, "rank"); 
     return articleURL; 
    } 

    private String readSourceURL(XmlPullParser parser) throws IOException, XmlPullParserException{ 
     parser.require(XmlPullParser.START_TAG, ns, "rank"); 
     String sourceURL  = readText(parser); 

     parser.require(XmlPullParser.END_TAG, ns, "rank"); 
     return sourceURL; 
    } 

    private String readImageURL(XmlPullParser parser) throws IOException, XmlPullParserException{ 
     parser.require(XmlPullParser.START_TAG, ns, "rank"); 
     String imageURL  = readText(parser); 

     parser.require(XmlPullParser.END_TAG, ns, "rank"); 
     return imageURL; 
    } 

    private String readText(XmlPullParser parser) throws IOException, XmlPullParserException { 
     String result = ""; 

     if (parser.next() == XmlPullParser.TEXT) { 
      result = parser.getText(); 
      parser.nextTag(); 
     } 
     return result; 
    } 


    private void skip(XmlPullParser parser) throws XmlPullParserException, IOException { 
     if (parser.getEventType() != XmlPullParser.START_TAG) { 
      throw new IllegalStateException(); 
     } 
     int depth = 1; 
     while (depth != 0) { 
      switch (parser.next()) { 
      case XmlPullParser.END_TAG: 
       depth--; 
       break; 
      case XmlPullParser.START_TAG: 
       depth++; 
       break; 
      } 
     } 
    } 
} 

我現在的問題是如何處理XML文件。特別是:如何讀取XML文件,以及如何讓該類爲文件執行工作並將其輸出到Android設備。將不勝感激任何幫助。更精確地說我有了這樣的佈局

<entry> 
<rank>Rank 1</rank> 
    <source><![CDATA[Der Postillon]]></source> 
    <date>2013-09-24 15:11:48</date> 
    <scores> 
     <Flies>10797</Flies> 
     <Facebook>10190</Facebook> 
     <Twitter>345</Twitter> 
     <GPlus>262</GPlus> 
    </scores> 
    <headline>Wikipedia löscht FDP-Eintrag wegen fehlender Relevanz</headline> 
    <description>Berlin (dpo) - Das ging schnell. Seit gestern existiert der Wikipedia-Eintrag der FDP nicht mehr. Der mit knapp 10.000 Wörtern durchaus umfangreiche Beitrag wurde nach einer kurzen Löschdiskussion entfernt, weil er den strengen Relevanzkriterien des Online-Lexikons nicht mehr standhalten konnte. Für marginale Splitterparteien und Kleinstgruppierungen wie die Liberalen sei kein Platz in der Wikipedia. [Weiterlesen]</description> 
    <articleURL>http://www.der-postillon.com/2013/09/wikipedia-loscht-fdp-eintrag-wegen.html</articleURL> 
    <sourceURL><![CDATA[http://www.der-postillon.com]]></sourceURL> 
    <imageURL><![CDATA[http://www.10000flies.de/images/nopic.jpg]]></imageURL> 
</entry> 

一個XML文件,但我似乎還沒有找到一個好的解決辦法處理這個問題。但我敢打賭,這很容易。我無法找到它。或者,你可以給我一個簡單的教程,介紹如何從本地文件夾讀取XML文件並使用它。

,如果你想從資產文件夾中的XML文件,然後在下面的代碼中使用
+0

的種類。我讀了另一個教程,並做到了這一點。不管怎麼說,還是要謝謝你。無論如何會給你正確的答案 – devShuba

回答

2

public void copyXMLFromAssets() 
    { 
     AssetManager assetManager = mActivity.getAssets(); 
     File AppDirectory = new File(Environment.getExternalStorageDirectory() + "/" + dirName); 
     if (!AppDirectory.exists()) 
      { 
       AppDirectory.mkdirs(); 
      } 
       InputStream in = null; 
       OutputStream out = null; 
       try 
        { 
         in = assetManager.open(XML_name); 
         File outFile = new File(AppDirectory +"/"+XML_name);         
         out = new FileOutputStream(outFile); 
         copyFile(in, out); 
         in.close(); 
         in = null; 
         out.flush(); 
         out.close(); 
         out = null; 
        } 
       catch (IOException e) 
        { 
         Log.e("tag", "Failed to copy asset file: " + XML_name, e); 
        } 

    } 

使用下面的代碼

public InputStream GetStreamFromXmlFileOnSDCard() 
     { 
      File file = new File(Environment.getExternalStorageDirectory() +xml_path); 
      InputStream istr = null; 
      try 
       { 
        istr = new FileInputStream(file); 
       } 
      catch (FileNotFoundException e) 
       { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      return istr; 

     } 

上述函數返回然後讀取保存的位置該複製XML文件用於xml文件的InputStream;你可以通過這個InputStream拉解析器和解析你的數據。

+0

不,我的意思是如何從項目中的文件夾中獲取xml文件,如/ res/xml,然後將它傳遞給XmlPullParser – devShuba

+0

我編輯了我的答案,看看它 – dd619