2013-12-17 72 views
0

以下格式是來自服務器的響應。我試圖通過使用DOM解析這個,但得到異常說「只允許一個根元素」。是否可以在Android中使用DOM解析器解析特定的XML?

<?xml version="1.0" encoding="UTF-8" ?> 
    <responseCode>2000</responseCode> 
    <responseText>Success</responseText> 
    <response> 
    <response1>on</response1> 
    <response1>on</response1> 
    <response1>on</response1> 
    <response1>on</response1> 
    <response1>on</response1> 
    </response> 
    <response> 
    <response1>on</response1> 
    <response1>on</response1> 
    <response1>on</response1> 
    <response1>on</response1> 
    <response1>on</response1> 
    </response> 
    <response> 
    <response1>on</response1> 
    <response1>on</response1> 
    <response1>on</response1> 
    <response1>on</response1> 
    <response1>on</response1> 
    </response> 
+1

yes.but你缺少一個根標籤。也推薦使用xmlpullparser http://developer.android.com/training/basics/network-ops/xml.html – Raghunandan

+0

@Raghunandan:嗨,感謝您的回覆,如果您有示例代碼,請發給我。 – user3110163

回答

0

這將是使用XmlPullParser更容易解析,並更有效的太

例。 XML文件內容(保存在外部存儲作爲code.xml)

<?xml version="1.0" encoding="utf-8"?> 
<countries> 
<country> 
    <name>Iran</name> 
    <phonecode>+98</phonecode> 
    <code>IRI</code> 
</country> 
<country> 
    <name>United State</name> 
    <phonecode>+1</phonecode> 
    <code>USA</code> 
</country> 
</countries> 

讓我們開始分析它

  1. 首先創建一個輔助類

    import android.util.Xml; 
    import org.xmlpull.v1.XmlPullParser; 
    import org.xmlpull.v1.XmlPullParserException; 
    import java.io.IOException; 
    import java.io.InputStream; 
    
    public class XmlDataParseHelper { 
    
    private XmlPullParser parser; 
    private static final String NULL = null; 
    
    /** 
    * 
    * @param in 
    * @throws XmlPullParserException 
    * @throws IOException 
    * @throws IllegalArgumentException 
    */ 
    public XmlDataParseHelper(InputStream in) throws XmlPullParserException, 
         IOException, IllegalArgumentException { 
        try { 
         parser = Xml.newPullParser(); 
         parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false); 
         parser.setInput(in, null); 
         parser.nextTag(); 
        } catch (Exception e) { 
         e.printStackTrace(); 
        } 
    } 
    
    /** 
    * 
    * @return XmlPullParser 
    */ 
    public XmlPullParser getParser() { 
        return parser; 
    } 
    
    /** 
    * 
    * @param parser 
    * @param tag 
    * @return String 
    * @throws IOException 
    * @throws XmlPullParserException 
    */ 
    public static String readTag(XmlPullParser parser, String tag) 
         throws IOException, XmlPullParserException { 
        String tagData = ""; 
        parser.require(XmlPullParser.START_TAG, NULL, tag); 
        if (parser.next() == XmlPullParser.TEXT) { 
         tagData = parser.getText(); 
         parser.nextTag(); 
        } 
        parser.require(XmlPullParser.END_TAG, NULL, tag); 
        return tagData; 
    } 
    
    /** 
    * 
    * @param parser 
    * @param tag 
    * @param attributeName 
    * @return String 
    * @throws IOException 
    * @throws XmlPullParserException 
    */ 
    public static String readAttribute(XmlPullParser parser, String tag, 
         String attributeName) throws IOException, XmlPullParserException { 
        parser.require(XmlPullParser.START_TAG, NULL, tag); 
        String attributeData = parser.getAttributeValue(NULL, attributeName); 
        parser.nextTag(); 
        parser.require(XmlPullParser.END_TAG, NULL, tag); 
        return attributeData; 
    } 
    
    } 
    
  2. 讀取XML文件as

    public InputStream readFile(String fileName) throws FileNotFoundException, 
         IOException { 
    //check external storage present 
    // else throw new IOException(); 
    
    return new FileInputStream(Environment.getExternalStorageDirectory() 
         + "/" + fileName); 
    } 
    
  3. 傳遞的InputStream它初始化XMLPullParser對象的另一種方法

    public void readXmlFile(String fileName) { 
    try { 
        if (fileName.isEmpty()) 
        throw new NullPointerException(); 
        readData(new XmlDataParseHelper(readFile(fileName)).getParser()); 
    } catch (IOException e) { 
         e.printStackTrace(); 
    } catch (XmlPullParserException e) { 
         e.printStackTrace(); 
    } catch (IllegalArgumentException e) { 
         e.printStackTrace(); 
    } 
    } 
    
  4. Atlast解析XMLPullParser object作爲

    public void readData(XmlPullParser parser) 
          throws XmlPullParserException, IOException { 
    int eventType = parser.getEventType(); 
    String tagName = ""; 
    Log.w("Developer", "Reading file..."); 
    while (eventType != XmlPullParser.END_DOCUMENT) { 
        switch (eventType) { 
         case XmlPullParser.START_DOCUMENT : 
          Log.w("Developer", "Reading backup file..."); 
          break; 
         case XmlPullParser.START_TAG : 
          tagName = parser.getName(); 
          if (tagName.equals("countries")) { 
           Log.w("XMLParse", "Start TAG countries"); 
           // do something when countries tag starts 
          } 
          if (tagName.equals("country")) { 
           Log.w("XMLParse", "Start TAG country"); 
           // do some when country tag starts 
          } else if (tagName.equals("name")) { 
           // read tag value using XmlDataParseHelper class 
           String countryName = XmlDataParseHelper.readTag(parser, 
               "name"); 
           Log.w("XmlParser", "Country name : "+countryName); 
          } else if (tagName.equals("phonecode")) { 
           String countryPhoneCode = 
               XmlDataParseHelper.readTag(parser,"phonecode"); 
           Log.w("XmlParser", "Country Phone code : "+countryPhoneCode); 
          } else if (tagName.equals("code")) { 
           String countryCode = 
              XmlDataParseHelper.readTag(parser, "code"); 
           Log.w("XmlParser", "Country code name : "+countryCode); 
          } 
         break; 
         case XmlPullParser.END_TAG : 
          tagName = parser.getName(); 
          if (tagName.equals("countries")) { 
            // do something when counties tag is close. 
          }   
         break; 
        } 
        eventType = parser.next(); 
    } 
    Log.w("Developer", "File parsing complete..."); 
    } 
    
  5. 要讀取文件只要調用方法readXmlFile(String fileName)在的AsyncTask的名稱存儲在外部存儲器中的文件。

完整的Android工作示例代碼是在這裏https://gist.github.com/rachitrm/7810837或叉子項目http://www.github.com/rachitrm/rm-xmlparser/

+0

嗨請看我的帖子一旦我的xml響應沒有根元素是否可以使用xml解析器解析 – user3110163

+0

@ user3110163是的,嘗試上面的代碼,就像XmlPullParser解析在開始文件和結束文件的基礎上。下載該項目並嘗試它...!我也在測試你的代碼。 –

+0

thnakyou洙很多我會嘗試它 – user3110163

2

這是無效的XML。該反應應該是如下:

<root> 
    <responseCode>2000</responseCode> 
    <responseText>Success</responseText> 
    <response> 
    <response1>on</response1> 
    <response1>on</response1> 
    <response1>on</response1> 
    <response1>on</response1> 
    <response1>on</response1> 
    </response> 
    <response> 
    <response1>on</response1> 
    <response1>on</response1> 
    <response1>on</response1> 
    <response1>on</response1> 
    <response1>on</response1> 
    </response> 
    <response> 
    <response1>on</response1> 
    <response1>on</response1> 
    <response1>on</response1> 
    <response1>on</response1> 
    <response1>on</response1> 
    </response> 
</root> 
+0

謝謝你的回覆是否有可能解析我的xml – user3110163