2009-09-03 104 views
5

如何解析位於我的android項目(res/XML文件夾內)中的本地XML文件並希望獲取該文件中的值。在Android中解析本地XML文件

+0

代碼,你可以檢查此鏈接也 http://stackoverflow.com/questions/9915219/parse-a-local-xml-file-and-store-in-sqlite-database-in-android – surhidamatya 2012-09-18 10:36:51

+0

http://stackoverflow.com/questions/5702729/store-parsed-xml-data-to-sqlite -android也檢查它。它可以幫助你 – surhidamatya 2012-09-18 10:39:50

回答

10

要訪問存儲在res/xml中的XML資源,請從任何Activity或其他Context調用getResources().getXml()。您需要提供getXml()要載入的XML的編號(R.xml.myfile)。

+6

getXml()方法自動返回你一個XmlResourceParser實現實例,所以你可以直接將它傳遞給XmlPullParser並開始解析你的數據。 – 2009-09-03 11:50:22

4

讀取你的XML代碼添加如下圖所示

XmlResourceParser myxml = mContext.getResources().getXml(R.xml.MyXml); 
//MyXml.xml is name of our xml in newly created xml folder, mContext is the current context 
// Alternatively use: XmlResourceParser myxml = getContext().getResources().getXml(R.xml.MyXml); 

myxml.next();//Get next parse event 
int eventType = myxml.getEventType(); //Get current xml event i.e., START_DOCUMENT etc. 

,並獲得的代碼添加內容如下所示