Casting node to element giving ClassCastException

前端 未结 2 1663
慢半拍i
慢半拍i 2020-12-21 00:02

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


        
相关标签:
2条回答
  • 2020-12-21 00:59

    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();
    
    }
    
    0 讨论(0)
  • 2020-12-21 00:59

    If node is element then only cast it. Make check like below.

    if (first.getNodeType() == Node.ELEMENT_NODE) { Element nm=(Element)first;}
    
    0 讨论(0)
提交回复
热议问题