检查xml中的资源并在android中显示资源的图标

时间:2010-12-20 15:23:56

标签: android

每个人都是。我是android的新手。在我的项目中,我在阅读xml文件时遇到了一些问题。在我的xml中,我已经包含了一些音频和视频路径,我想通过代码读取xml文件,如果有一些音频或视频文件,我想在我的视图中显示一些图像。任何机构都可以tel如何读取xml文件。  提前感谢你

1 个答案:

答案 0 :(得分:1)

好的,首先你需要创建一个解析器下面是执行此操作的代码:

public static void readTemplateFile(Context context){

    /**
     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。

FileReader(new File(Environment.getExternalStorageDirectory()。getPath()+“/ FINILATH / FILE.XML”));

以下是XML_Handler_Class,它是空白的:

import 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)我将一个上下文传递给了班级,所以在我学习的时候,我可以使用toasts来向我展示正在阅读的值。