解析/res/xml下的xml文件
android xml目录下的文件主要用于设置界面的配置信息,但是我们也可以将一些东西存放到xml目录下,例如MIME TYPE, 废话不多说,直接上代码.
<?xml version="1.0" encoding="utf-8"?> <MimeTypes> <type extension=".3gp" mimetype="video/3gpp" /> <type extension=".apk" mimetype="application/vnd.android.package-archive" /> <type extension=".asf" mimetype="video/x-ms-asf" /> <type extension=".avi" mimetype="video/x-msvideo" /> <type extension=".bin" mimetype="application/octet-stream" /> <type extension=".bmp" mimetype="image/bmp" /> <type extension=".c" mimetype="text/plain" /> <type extension=".class" mimetype="application/octet-stream" /> <type extension=".conf" mimetype="text/plain" /> <type extension=".cpp" mimetype="text/plain" /> <type extension=".csv" mimetype="text/csv" /> <type extension=".doc" mimetype="application/msword" /> <type extension=".docx" mimetype="application/msword" /> <type extension=".exe" mimetype="application/octet-stream" /> <type extension=".gif" mimetype="image/gif" /> <type extension=".gtar" mimetype="application/x-gtar" /> <type extension=".gz" mimetype="application/gzip" /> <type extension=".h" mimetype="text/plain" /> <type extension=".htm" mimetype="text/html" /> <type extension=".html" mimetype="text/html" /> <type extension=".jar" mimetype="application/java-archive" /> <type extension=".java" mimetype="text/plain" /> <type extension=".jpeg" mimetype="image/jpeg" /> <type extension=".jpg" mimetype="image/jpeg" /> <type extension=".js" mimetype="application/x-javascript" /> <type extension=".log" mimetype="text/plain" /> <type extension=".m3u" mimetype="audio/x-mpegurl" /> <type extension=".m4a" mimetype="audio/mp4a-latm" /> <type extension=".m4b" mimetype="audio/mp4a-latm" /> <type extension=".m4p" mimetype="audio/mp4a-latm" /> <type extension=".m4u" mimetype="video/vnd.mpegurl" /> <type extension=".m4v" mimetype="video/x-m4v" /> <type extension=".mid" mimetype="audio/mid" /> <type extension=".midi" mimetype="audio/midi" /> <type extension=".mkv" mimetype="video/*" /> <type extension=".mov" mimetype="video/quicktime" /> <type extension=".mp2" mimetype="audio/x-mpeg" /> <type extension=".mp3" mimetype="audio/mp3" /> <type extension=".mp4" mimetype="video/mp4" /> <type extension=".mpc" mimetype="application/vnd.mpohun.certificate" /> <type extension=".mpe" mimetype="video/mpeg" /> <type extension=".mpg" mimetype="video/mpeg" /> <type extension=".mpeg" mimetype="video/mpeg" /> <type extension=".mpga" mimetype="audio/mpeg" /> <type extension=".msg" mimetype="application/vnd.ms-outlook" /> <type extension=".ogg" mimetype="audio/x-ogg" /> <type extension=".pdf" mimetype="application/pdf" /> <type extension=".php" mimetype="text/php " /> <type extension=".png" mimetype="image/png" /> <type extension=".pps" mimetype="application/vnd.ms-powerpoint" /> <type extension=".ppt" mimetype="application/vnd.ms-powerpoint" /> <type extension=".pptx" mimetype="application/vnd.ms-powerpoint" /> <type extension=".prop" mimetype="text/plain" /> <type extension=".rar" mimetype="application/x-rar-compressed" /> <type extension=".rc" mimetype="text/plain" /> <type extension=".rmvb" mimetype="video/*" /> <type extension=".rtf" mimetype="application/rtf" /> <type extension=".sh" mimetype="text/plain" /> <type extension=".tar" mimetype="application/x-tar" /> <type extension=".tgz" mimetype="application/x-compressed" /> <type extension=".txt" mimetype="text/plain" /> <type extension=".vob" mimetype="video/*" /> <type extension=".wma" mimetype="audio/x-ms-wma" /> <type extension=".wav" mimetype="audio/wav" /> <type extension=".xml" mimetype="text/xml" /> <type extension=".wmv" mimetype="video/*" /> <type extension=".wps" mimetype="application/vnd.ms-works" /> <type extension=".xls" mimetype="application/vnd.ms-excel" /> <type extension=".xlsx" mimetype="application/vnd.ms-excel" /> <type extension=".z" mimetype="application/x-compressed" /> <type extension=".zip" mimetype="application/zip" /> </MimeTypes>
XmlUtils.java
public class XmlUtils { private static final String TAG = "XmlUtils"; public static String getMimeType(Context context, String extension) { if (extension == null || extension.trim().length() == 0) { throw new IllegalArgumentException("extension can not be null or it's length is 0"); } XmlResourceParser parser = context.getResources().getXml(R.xml.mimetypes); int eventType = 0; try { eventType = parser.getEventType(); } catch (XmlPullParserException e) { Log.e(TAG, "XmlPullParserException", e); e.printStackTrace(); } while (true) { if (eventType == XmlResourceParser.START_TAG) { if (parser.getName().equals("type") && extension.equals(parser.getAttributeValue(0))) { return parser.getAttributeValue(1); //如果要读取<A>TEXT</A>中的TEXT就使用parser.nextText(); } } else if (eventType == XmlResourceParser.END_DOCUMENT) { throw new RuntimeException(extension + " is not support!"); } try { eventType = parser.next(); } catch (Exception e) { Log.e(TAG, "", e); e.printStackTrace(); } } } }
这里主要使用的是android扩展XmlPullParser的XmlResourceParser
欢迎分享转载,转载请注明出处 http://www.cnblogs.com/fangyucun