0
我使用的是XML解析器拉讀取XML文件,我想讀DocDecl元素(在文件的開頭。XmlPullParser沒有看到DOCDECL元素
下面是我使用的代碼,
FileInputStream inputStream;
XmlPullParserFactory factory;
XmlPullParser xpp;
XmlNode document;
inputStream = new FileInputStream(file);
factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
factory.setFeature(XmlPullParser.FEATURE_PROCESS_DOCDECL, true);
xpp = factory.newPullParser();
xpp.setInput(inputStream, null);
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
switch (eventType) {
case XmlPullParser.START_DOCUMENT:
Log.i("Test XML", "START_DOCUMENT");
break;
case XmlPullParser.END_DOCUMENT:
Log.i("Test XML", "END_DOCUMENT");
break;
case XmlPullParser.START_TAG:
Log.i("Test XML", "START_TAG");
break;
case XmlPullParser.END_TAG:
Log.i("Test XML", "END_TAG");
break;
case XmlPullParser.TEXT:
Log.i("Test XML", "TEXT");
break;
case XmlPullParser.CDSECT:
Log.i("Test XML", "CDSECT");
break;
case XmlPullParser.ENTITY_REF:
Log.i("Test XML", "ENTITY_REF");
break;
case XmlPullParser.IGNORABLE_WHITESPACE:
Log.i("Test XML", "IGNORABLE_WHITESPACE");
break;
case XmlPullParser.PROCESSING_INSTRUCTION:
Log.i("Test XML", "PROCESSING_INSTRUCTION");
break;
case XmlPullParser.COMMENT:
Log.i("Test XML", "COMMENT");
break;
case XmlPullParser.DOCDECL:
Log.i("Test XML", "DOCDECL");
break;
default:
break;
}
eventType = xpp.nextToken();
}
而這裏的結果logcat的我得到:
START_DOCUMENT START_TAG START_TAG TEXT END_TAG START_TAG TEXT END_TAG COMMENT END_DOCUMENT
下列與XML
<?xml version="1.0" encoding="utf-8"?>
<root>
<item>Some text</item>
<item>Some other text</item>
<!-- Here's a comment -->
</root>
我使用的FEATURE_PROCESS_DOCDECL,還有的nextToken()就一定要讀到的一切,然而DocDecl不被處理。我錯過了什麼嗎?