简体   繁体   English

解析xml android的字符串

[英]Parsing string of xml android

I'm having a little trouble parsing a string of xml called responseText in android. 我在解析android中名为responseText的xml字符串时遇到了一些麻烦。 The xml is fully valid and has the following structure: xml是完全有效的,并且具有以下结构:

<plan>
<entry>
<name>john</name>
<address>uk</address>
</entry>
<entry>
<name>joe</name>
<address>usa</address>
</entry>
</plan>

The code I am using to parse the String is as follows: 我用来解析String的代码如下:

    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputSource is = new InputSource();

        is.setCharacterStream(new StringReader(responseText));

        Document doc = db.parse(is);
        NodeList nodes = doc.getElementsByTagName("entry");

        for (int i = 0; i < nodes.getLength(); i++) {

            Element element = (Element) nodes.item(i);

            NodeList name = ((Document) element)
                    .getElementsByTagName("name");
            Element line = (Element) name.item(0);
            Toast.makeText(Containers.this,
                    getCharacterDataFromElement(line), Toast.LENGTH_SHORT)
                    .show();

            NodeList title = ((Document) element)
                    .getElementsByTagName("address");
            line = (Element) title.item(0);
            Toast.makeText(Containers.this,
                    getCharacterDataFromElement(line), Toast.LENGTH_SHORT)
                    .show();

        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static String getCharacterDataFromElement(Element e) {
    Node child = ((Node) e).getFirstChild();
    if (child instanceof CharacterData) {
        CharacterData cd = (CharacterData) child;
        return cd.getData();
    }
    return "?";

}

I'm just using simple toasts to print the xml data to screen. 我只是使用简单的吐司将xml数据打印到屏幕上。 However, i get an IOexception when I enter the for loop. 但是,当我进入for循环时,我得到一个IOexception。 Any idea what is wrong? 知道有什么问题吗?

Are you importing the types from the correct packages? 您是从正确的包中导入类型吗? Something like 就像是

import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.CharacterData;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import android.sax.Element; // Wrong Element class

Change the last import to 将最后一次导入更改为

import org.w3c.dom.Element;

and try again. 然后再试一次。

I don't see anything that could cause an IOException inside the loop. 我看不到任何可能在循环内导致IOException的情况。

However, are you sure you can just go and cast an Element into a Document? 但是,您确定可以直接将元素投射到文档中吗? At any rate you shouldn't need to anyways, since Element also has the getElementsByTagName method. 无论如何,您都不需要,因为Element也具有getElementsByTagName方法。

尝试在字符串的开头添加xml声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM