Home > Article > Backend Development > Android code example for reading xml
/** * 从config.xml中获取版本信息以及应用id * * @param urlPath * @return * @throws Exception */ public List<String> getUpdateInfo() { Resources r = context.getResources(); // 通过Resources,获得XmlResourceParser实例 XmlResourceParser xrp = r.getXml(R.xml.config); List<String> list = new ArrayList<String>(); // 如果是开始标签 try { // 如果没有到文件尾继续执行 while (xrp.getEventType() != XmlResourceParser.END_DOCUMENT) { // 如果是开始标签 if (xrp.getEventType() == XmlResourceParser.START_TAG) { // 获取标签名称 String name = xrp.getName(); // 判断标签名称是否等于friend if (name.equals("update")) { // 获得标签属性追加到StringBuilder中 list.add(xrp.getAttributeValue(0)); list.add(xrp.getAttributeValue(1)); list.add(xrp.getAttributeValue(2)); } } else if (xrp.getEventType() == XmlPullParser.END_TAG) { } else if (xrp.getEventType() == XmlPullParser.TEXT) { } // 下一个标签 xrp.next(); } } catch (XmlPullParserException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return list; }
The above is the detailed content of Android code example for reading xml. For more information, please follow other related articles on the PHP Chinese website!