2013-12-17 74 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