2010-12-20 53 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