2010-12-20 58 views
0

hai每一個。我是Android新手。在我的項目中,我閱讀XML文件時遇到了一些問題。在我的xml中,我包含了一些音頻和視頻路徑,並且我想通過代碼讀取xml文件,並且如果有一些音頻或視頻文件,我想在我的視圖中顯示一些圖像。可以在任何機構的電話如何閱讀XML文件。 請多關照檢查xml中的資源並顯示android中的資源的圖標

回答

1

好首先你需要創建一個解析器下面是代碼來做到這一點:

公共靜態無效readTemplateFile(上下文的背景下){

/** 
    Include File Checking 
    */ 

    try { 
     XML_Handler_Template myExHan = new XML_Handler_Template(); 

     InputStreamReader isr = new 
     FileReader(new File(Environment.getExternalStorageDirectory().getPath() + "/Library Template.xml")); 

     XML_Handler_Template.context = context; 

     SAXParserFactory spf = SAXParserFactory.newInstance(); 
     SAXParser sp = spf.newSAXParser(); 

     XMLReader xr = sp.getXMLReader(); 

     xr.setContentHandler((ContentHandler) myExHan); 

     xr.parse(new InputSource(isr)); 


    } catch (Exception e) { 

     Toast.makeText(context, ">" + e.getMessage(), Toast.LENGTH_LONG).show(); 


    } 

} 

然後,您需要一個處理程序類。在上面的例子中,我的類被稱爲XML_Handler_Template。 (新文件(Environment.getExternalStorageDirectory()。getPath()+「/FILEPATH/FILE.XML」));}};}};}}

這裏是此刻卻是空白的XML_Handler_Class:

進口org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler;

公共類XML_Handler_Template擴展了DefaultHandler {

public static Context context = null; 

@Override 
public void startDocument() throws SAXException { 
    //this is called when the document is first read 
} 

@Override 
public void startElement(String namespaceURI, String localName, 
     String qName, Attributes atts) throws SAXException { 
        //This is called when a new Tag is opened 
        //localName holds the Tag Name, the Value is got from the 
        //Characters function at the end of this class 

        //the attributes for each tag are stored in the atts array, you can either handle the attribute values here or pass the information to a separate function to handle them, 
        if (atts.getLength()>0){ 
      for (int i=0;i<atts.getLength();i++){ 

       addAttrib(atts.getLocalName(i) , atts.getValue(i)) ; 

      } 
     } 
} 

@Override 
public void endElement(String namespaceURI, String localName, String qName) 
throws SAXException { 
    //This is called when a Tag is closed 
} 

@Override 
public void endDocument() throws SAXException { 
       //this is called when the document is closed 
} 

@Override 
public void characters(char ch[], int start, int length) { 
      //This is where the value of a Tag are read 
    String value = new String(ch , start , length); 
      // You may want to include a replaceAll("\r","") and replaceAll("\n","") to remove any hidden chars 

}  

}

有這樣的發揮,看看你的身體情況如何,現在= 0),我通過上下文的類,所以雖然我是學我可以用敬酒向我展示正在閱讀的價值。

+0

k謝謝。我有存儲在SD卡上,我想顯示相關的音頻\視頻圖標。 – user493244 2010-12-21 08:11:28

+0

好吧有幾件事情需要實施 – 2010-12-21 20:36:58

+0

好吧,有很多事情需要你去處理。 1. XML解析 2.可繪圖(不同設備需要的圖標和規格) 3.視圖和充氣器。添加自定義ListItems到列表視圖或動態添加視圖到ScrollView 你是否熟悉上述任何一個? – 2010-12-21 20:47:21