XML文档内容如下
<?xml version="1.0" encoding="GB2312"?><PhoneInfo>
<Brand name="联想"><Type name="A60"/></Brand><Brand name="苹果"><Type name="iPhone4"/></Brand></PhoneInfo>
读取XM文档的JAVA源构码如下: public class Test { /**
 * @param args
 */
public static void main(String[] args) {
//1、得到DOM解析器的工厂实例
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
//2、从DOM工厂获得DOM解析器
try {
DocumentBuilder db=dbf.newDocumentBuilder();

//3、解析xml文档,得到一个Document,即DOM树
Document doc=db.parse("src/收藏信息.xml");
//4、得到所有Brand节点列表信息
NodeList list=doc.getElementsByTagName("Brand");
//5、循环Brand信息
for(int i=0;i<list.getLength();i++){
Node brandNode=list.item(i);
                                     //6、强制转换得到全部的元素
Element brandElement=(Element) brandNode;
String brandName=brandElement.getAttribute("name");
System.out.println(brandName);//输出手机品牌名

NodeList childList=brandNode.getChildNodes();
for(int j=0;j<childList.getLength();j++){
Element typeElement=(Element) childList.item(j);
String typeName=typeElement.getAttribute("name");

System.out.println("手机:"+brandName+typeName);
}
}

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();

}
}
请问在这段源代码的注释6下的强制转换是否不合理?因为Node brandNode=list.item(i);这段代码中得到的Brand全部节点brandNode也包含了Brand的属性节点,它怎么就能全部强制转换为元素Element类型的呢?事先声明这段代码没有报错,请各位大哥前辈帮帮小弟吧~~~~~~~~谢了哦^_^

解决方案 »

  1.   

    没问题!Element req = (Element) doc.selectSingleNode("//REQUEST_INFO");
    for (Iterator i = req.elementIterator(); i.hasNext();) {
       Element el = (Element) i.next();
    }
      

  2.   

    大哥我想问下在源代码中第二个for循环上面NodeList childList=brandNode.getChildNodes();
    这个childList得到的是Brand下的全部子节点,也就是说包含元素type和type的属性name
    ;它怎么能在第二个for循环中用这句Element typeElement=(Element) childList.item(j)全部给它转换成Element类型的呢?谢谢啦,就是这点不明白
      

  3.   

    NodeList list=doc.getElementsByTagName("Brand");这里用的getElementsByTagName 就只选中了所有Brand的Element 就不包含其它属性节点或者文本节点了啊
      

  4.   

    那在第二个for循环中呢?第二个for循环中用的是getChildNodes()方法啊,这个是得到全部节点了吧?包括属性的在源代码中第二个for循环上面NodeList childList=brandNode.getChildNodes();
    这个childList得到的是Brand下的全部子节点,也就是说包含元素type和type的属性name
    ;它怎么能在第二个for循环中用这句Element typeElement=(Element) childList.item(j)全部给它转换成Element类型的呢?谢谢啦,就是这点不明白
      

  5.   

    <Brand name="联想"><Type name="A60"/></Brand>
    <Brand name="苹果"><Type name="iPhone4"/></Brand>
    如果Brand和子元素没有加回车  那就正确啊 中间如果加了回车 比如这样
    <Brand name="联想">
      <Type name="A60"/>
    </Brand>
    那就应该会多两个空的文本节点了
      

  6.   

    package cn.dom;import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    import org.xml.sax.SAXException;public class Demo {
    public static void main(String[] args) {
     try {
    DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
    DocumentBuilder db=dbf.newDocumentBuilder();
    Document doc=db.parse("dom/info.xml");
    NodeList brandlist=doc.getElementsByTagName("Brand");
    System.out.println("手机收藏信息");
    System.out.println("************************");
    for(int i=0;i<brandlist.getLength();i++)
    {
    Node breadlist= brandlist.item(i);
      Element breadelement =(Element) breadlist;
      String breadName=breadelement.getAttribute("name");
      NodeList typelist=breadelement.getChildNodes();
      for(int j=0;j<typelist.getLength();j++)
      {
    Element typeelement=  (Element) typelist.item(j);
    String typeName=typeelement.getAttribute("name");
    System.out.println("[手机品牌:"+breadName+typeName+"]");
      System.out.println("************************");
      }
    }
    } catch (ParserConfigurationException e) {
    e.printStackTrace();
    } catch (SAXException e) {
    e.printStackTrace();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
    我的楼主的一样,可是我一运行它不报错,错误如下:java.lang.ClassCastException: com.sun.org.apache.xerces.internal.dom.DeferredTextImpl cannot be cast to org.w3c.dom.Element
    at cn.dom.Demo.main(Demo.java:30)
    ..........求解。
      

  7.   

    package cn.dom;import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    import org.xml.sax.SAXException;public class Demo {
    public static void main(String[] args) {
     try {
    DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
    DocumentBuilder db=dbf.newDocumentBuilder();
    Document doc=db.parse("dom/info.xml");
    NodeList brandlist=doc.getElementsByTagName("Brand");
    System.out.println("手机收藏信息");
    System.out.println("************************");
    for(int i=0;i<brandlist.getLength();i++)
    {
    Node breadlist= brandlist.item(i);
      Element breadelement =(Element) breadlist;
      String breadName=breadelement.getAttribute("name");
      NodeList typelist=breadelement.getChildNodes();
      for(int j=0;j<typelist.getLength();j++)
      {
    Element typeelement=  (Element) typelist.item(j);
    String typeName=typeelement.getAttribute("name");
    System.out.println("[手机品牌:"+breadName+typeName+"]");
      System.out.println("************************");
      }
    }
    } catch (ParserConfigurationException e) {
    e.printStackTrace();
    } catch (SAXException e) {
    e.printStackTrace();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
    我的楼主的一样,可是我一运行它不报错,错误如下:java.lang.ClassCastException: com.sun.org.apache.xerces.internal.dom.DeferredTextImpl cannot be cast to org.w3c.dom.Element
    at cn.dom.Demo.main(Demo.java:30)
    ..........求解。
      

  8.   

    用的 是  dom4j 吗? 我下面这个是用的dom4j
    Element root = doc.getRootElement();
    Iterator it = root.elementIterator("Brand");while(it.hasNext()) {
       Attribute attribute = ((Element)it.next()).attribute("name");
       System.out.println(attribute.getValue());
    }
      

  9.   

    你们好像用的都不是dom4j ....囧
      

  10.   

    解释一下:NodeList brandlist=doc.getElementsByTagName("Brand");//获得的是名称为brand的所有节点,不包含brand节点下的子节点(问题在这),不包含Type节点。还是用dom4j好