XML解析类强制转换异常

时间:2015-12-16 05:09:45

标签: android xml-parsing classcastexception

<xml>
<Table_Info>
<SeenStatus>0</SeenStatus>
<NewLimit>0</NewLimit>
</Table_Info>
</xml>

这是我从服务器端获取的数据,解析它的代码如下 -

            DocumentBuilderFactory dbf = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(new InputSource(url.openStream()));
            doc.getDocumentElement().normalize();

            NodeList nodeList = doc.getElementsByTagName("Table_Info");

            /** Assign textview array length by arraylist size */

            System.out.println(".....prinyt user data..." + url);

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

                Node node = nodeList.item(i);

                Element fstElmnt = (Element) node;

                NodeList costList1 = fstElmnt.getElementsByTagName("SeenStatus");
                Element costElement1 = (Element) costList1.item(0);
                costList1 = costElement1.getChildNodes();
                seen_stat_server = ((Node) costList1.item(0)).getNodeValue();
                String mchaal = min_chaal;
                System.out.println("...prinyt user data....." + min_chaal);
                NodeList costList = fstElmnt.getElementsByTagName("NewLimit");
                Element costElement = (Element) costList1.item(0);
                costList = costElement.getChildNodes();

                min_chaal = ((Node) costList1.item(0)).getNodeValue();
                System.out.println("...prinyt user data....." + total);
                String str2 = total;

现在我得到例外 -

  

java.lang.ClassCastException:org.apache.harmony.xml.dom.TextImpl无法强制转换为org.w3c.dom.Element

在这一行 - &gt;

`NodeList costList = fstElmnt.getElementsByTagName("NewLimit");
                    Element costElement = (Element) costList1.item(0);` 

实际上我是xml解析的新手,所以请告诉我这是什么问题

1 个答案:

答案 0 :(得分:0)

我建议你以比SAX和DOM解析器更快的方式使用XMLPullParser。

下面的

是XMLPullParserHandler类 -

public class XmlPullParserHandler {

String text;
StringBuilder builder = new StringBuilder();

public String parse(InputStream is) {

    try {

        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser parser = factory.newPullParser();

        parser.setInput(is, null);

        int eventType = parser.getEventType();

        while (eventType != XmlPullParser.END_DOCUMENT) {
            String tagname = parser.getName();
            switch (eventType) {

                case XmlPullParser.TEXT:
                    text = parser.getText();
                    break;

                case XmlPullParser.END_TAG:
                    if (tagname.equalsIgnoreCase("SeenStatus")) {
                        builder.append("SeenStatus = " + text);
                    } else if (tagname.equalsIgnoreCase("NewLimit")) {
                        builder.append("\nNewLimit = " + text);
                    }
                    break;

                default:
                    break;
            }
            eventType = parser.next();
        }

    } catch (IOException e) {
        e.printStackTrace();
    } catch (XmlPullParserException e) {
        e.printStackTrace();
    }

    return builder.toString();
}


}
下面的

是XMLPullParser类

public class XMLPullParser extends AppCompatActivity {

TextView txtXML;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_xmlpull_parser);


    txtXML = (TextView) findViewById(R.id.txtXML);

    try {

        XmlPullParserHandler parser = new XmlPullParserHandler();
        InputStream is = getAssets().open("sampleXML.xml");

        String xmlOutput = parser.parse(is);
        txtXML.setText(xmlOutput);

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


}

}

我从资产中获取了xml但是你可以简单地传递返回的响应Inputstream来成功解析它。