铸造节点元素给抛出ClassCastException [英] Casting node to element giving ClassCastException
本文介绍了铸造节点元素给抛出ClassCastException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在这里N2是我的节点列表,我只是想看看我的根元素的第一个子节点
公共无效ClickMe(查看视图){
节点rootElement的= n2.item(0);
节点列表的孩子= rootElement.getChildNodes();
节点首先= child.item(0);
// ClassCastException异常错误来了,每当我投第一个元素。
元素纳米=(元)第一;
选项Q = getOption(纳米,第一);
Log.i(TAG的名字是在谈论的是:+ q.getName());
}
这是什么logcat的说
07-31 20:32:38.376:E / AndroidRuntime(2950):java.lang.ClassCastException:产生的原因org.apache.harmony.xml.dom.TextImpl不能转换为org.w3c.dom.Element中
解决方案
试试这样说......
节点列表LOP = odoc.getElementsByTagName(Your_XML_Top_Element);
节点FPN = LOP.item(0);
尝试{
如果(FPN.getNodeType()== Node.ELEMENT_NODE)
{
元素标记=(元)FPN;
节点列表oNameList1 = token.getElementsByTagName(Your_XML_Sub_Node);
元件firstNameElement =(元件)oNameList1.item(0);
节点列表textNList1 = firstNameElement.getChildNodes();
}
here n2 is my NodeList, and i just want to see the first child node of my root element
public void ClickMe(View view){
Node rootElement=n2.item(0);
NodeList child=rootElement.getChildNodes();
Node first=child.item(0);
//ClassCastException error is coming whenever i am casting first to Element.
Element nm=(Element)first;
Option q= getOption(nm,first);
Log.i(TAG,"the name is was talking about is : "+ q.getName());
}
this what logcat says
07-31 20:32:38.376: E/AndroidRuntime(2950): Caused by: java.lang.ClassCastException: org.apache.harmony.xml.dom.TextImpl cannot be cast to org.w3c.dom.Element
解决方案
Try it like this....
NodeList LOP = odoc.getElementsByTagName("Your_XML_Top_Element");
Node FPN =LOP.item(0);
try{
if(FPN.getNodeType() == Node.ELEMENT_NODE)
{
Element token = (Element)FPN;
NodeList oNameList1 = token.getElementsByTagName("Your_XML_Sub_Node");
Element firstNameElement = (Element)oNameList1.item(0);
NodeList textNList1 = firstNameElement.getChildNodes();
}
这篇关于铸造节点元素给抛出ClassCastException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文