2012-09-20 48 views
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不被處理。我錯過了什麼嗎?

回答

0

我在xml文件的開始處誤認爲標記的DOCDECL標記,但它用於DOCTYPE聲明。

爲了達到我想要的東西,得到的版本,編碼和這樣從XML,下面的代碼可用於:

public static final String PROPERTY_XML_VERSION = "http://xmlpull.org/v1/doc/properties.html#xmldecl-version"; 
public static final String PROPERTY_XML_STANDALONE = "http://xmlpull.org/v1/doc/properties.html#xmldecl-standalone"; 

// [...] 

int eventType = xpp.getEventType(); 

while (eventType != XmlPullParser.END_DOCUMENT) { 
} 

String version = (String) xpp.getProperty(PROPERTY_XML_VERSION); 
Boolean standalone = (Boolean) xpp.getProperty(PROPERTY_XML_STANDALONE); 
String encoding = xpp.getInputEncoding(); 

我應該有更好的閱讀XmlPullParser Doc