android 读取assets中XML文件

时间:2024-03-21 09:39:48

 

android 读取assets中XML文件

 

android 读取assets中XML文件

 

 

android 读取assets中XML文件

 

android 读取assets中XML文件

 

 

 

代码如下:

//读取XML
public static List<VuforiaImageTarget> readXML(InputStream inStream) {
    XmlPullParser parser = Xml.newPullParser();
    try {
        parser.setInput(inStream, "UTF-8");
        int eventType = parser.getEventType();
        VuforiaImageTarget currentPerson = null;
        List<VuforiaImageTarget> persons = null;
        while (eventType != XmlPullParser.END_DOCUMENT) {
            switch (eventType) {
                case XmlPullParser.START_DOCUMENT://文档开始事件,可以进行数据初始化处理
                    persons = new ArrayList<VuforiaImageTarget>();
                    break;
                case XmlPullParser.START_TAG://开始元素事件
                    String name = parser.getName();
                    if (name.equalsIgnoreCase("ImageTarget")) {
                        currentPerson = new VuforiaImageTarget();
                        currentPerson.name = parser.getAttributeValue(null, "name");// 如果后面是Text元素,即返回它的值
                        currentPerson.size = parser.getAttributeValue(null, "size");}
                    break;
                case XmlPullParser.END_TAG://结束元素事件
                    LogUtil.d(LOGTAG, "tag_end:" + parser.getName());
                    if (parser.getName().equalsIgnoreCase("ImageTarget") && currentPerson != null) {
                        persons.add(currentPerson);
                        currentPerson = null;}
                    break;}
            eventType = parser.next();}
        inStream.close();
        return persons;
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}