最近學習Android時,需要用到解析XML文件里的數據,可以用XmlResourceParser來解析xml文件,正好將此記錄下來。
XmlResourceParser里常用的字段和方法
首先先給出源碼里面一些比較基礎的,常用的方法和字段。
常用的字段
int START_DOCUMENT = 0;
int END_DOCUMENT = 1;
int START_TAG = 2;
int END_TAG = 3;
int TEXT = 4;
getEventType()
/**
* Returns the type of the current event (START_TAG, END_TAG, TEXT, etc.)
* 大意就是返回當前的事件類型(返回的字段都是xml文件中某些特定位置,比如標簽開始標志,標簽結束標志,文檔結束標志等)
*
*/
int getEventType();
getName()
/**
* For START_TAG or END_TAG events, the (local) name of the current
* element is returned when namespaces are enabled. When namespace
* processing is disabled, the raw name is returned.
* 大意就是對於 START_TAG,END_TAG,這兩種事件,有無使用命名空間情況下返回的標簽名。至於命名空間的詳情,可以去參考xml的具體介紹(囧:我也不懂)
*
*/
String getName();
getText()
/**
* Returns the text content of the current event as String.
* 返回text內容
*
*/
String getText();
getAttributeName(int index)
/**
* Returns the local name of the specified attribute
* if namespaces are enabled or just attribute name if namespaces are disabled.
* 大意就是返回指定位置的屬性名,位置從0開始
*
* @param index zero-based index of attribute
* @return attribute name (null is never returned)
*/
String getAttributeName(int index);
getAttributeValue(int index)
/**
* Returns the given attributes value.
* 大意就是返回指定位置的屬性值,位置從0開始
*
* @param index zero-based index of attribute
* @return value of attribute (null is never returned)
*/
String getAttributeValue(int index);
getAttributeValue(String namespace,String name)
/**
* Returns the attributes value identified by namespace URI and namespace localName.
* If namespaces are disabled namespace must be null.
* 大意就是返回指定的屬性名對應的屬性值,如果沒有使用命名空間,則第一個參數傳入null
*
* @param namespace Namespace of the attribute if namespaces are enabled otherwise must be null
* @param name If namespaces enabled local name of attribute otherwise just attribute name
* @return value of attribute or null if attribute with given name does not exist
*/
String getAttributeValue(String namespace,String name);
next()
/**
* Get next parsing event - element content will be coalesced and only one
* TEXT event must be returned for whole element content
* 大意就是獲取下一個要解析的事件,通俗點說就是類似於將光標往下移
*/
int next()
對於一些基礎的操作,上述提供的信息就夠用了。至於每個字段,方法都是干嘛用的,其實也就是字面上的意思。下面先上一張圖:
XmlResourceParser具體如何解析xml不清楚,但解析過程有點類似於sqlite cursor
遍歷。首先都是初始定位在文檔開始處,通過調用 next()
來將光標往下移,通過 getEventType()
來獲取當前光標停留在哪里,然后再通過對應的 get××××()
方法來獲取我們想要的數據。
實例
首先在res/
目錄下建一個xml文件夾,然后新建一個xml文件命名為xml.xml:
<?xml version="1.0" encoding="utf-8"?>
<xml>
<Node att1="hello" att2="world"/>
HelloWorld!
</xml>
然后是java代碼,布局文件就一個按鈕控件:
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private Button btn1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
logXmlData();
}
});
}
public void logXmlData() {
XmlResourceParser xmlParser = getResources().getXml(R.xml.xml);
try {
int event = xmlParser.getEventType(); //先獲取當前解析器光標在哪
while (event != XmlPullParser.END_DOCUMENT){ //如果還沒到文檔的結束標志,那么就繼續往下處理
switch (event){
case XmlPullParser.START_DOCUMENT:
Log.i(TAG,"xml解析開始");
break;
case XmlPullParser.START_TAG:
//一般都是獲取標簽的屬性值,所以在這里數據你需要的數據
Log.d(TAG,"當前標簽是:"+xmlParser.getName());
if (xmlParser.getName().equals("Node")){
//兩種方法獲取屬性值
Log.d(TAG,"第一個屬性:" + xmlParser.getAttributeName(0)
+ ": " + xmlParser.getAttributeValue(0));
Log.d(TAG,"第二個屬性:" + xmlParser.getAttributeName(1)+": "
+ xmlParser.getAttributeValue(null,"att2"));
}
break;
case XmlPullParser.TEXT:
Log.d(TAG,"Text:" + xmlParser.getText());
break;
case XmlPullParser.END_TAG:
break;
default:
break;
}
event = xmlParser.next(); //將當前解析器光標往下一步移
}
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
打出的日志:
最近剛開通了公眾號,想激勵自己堅持寫作下去,初期主要分享原創的Android或Android-Tv方面的小知識,感興趣的可以點一波關注,謝謝支持~~