gpt4 book ai didi

java - 将节点转换为元素给出 ClassCastException

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:00:52 24 4
gpt4 key购买 nike

这里的 n2 是我的 NodeList,我只想看到我的根元素的第一个子节点

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());
}

这是 logcat 所说的

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

最佳答案

像这样尝试....

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();

}

关于java - 将节点转换为元素给出 ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11822719/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com