日期:2014-05-20  浏览次数:20773 次

急!!the method getTextContent() is undefined for the type Element
package   dom;
/**
  *   parse   xml   file
  *   @author   Administrator
  *
  */
import   javax.xml.parsers.*;
import   org.w3c.dom.*;

public   class   ParseXMLByDom   {

//get   document   object   by   parse   file
public   Document     getDocumentByFile()throws   Exception{
DocumentBuilderFactory   dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder   db=dbf.newDocumentBuilder();
Document   doc=db.parse( "student.xml ");
return   doc;
}
//parse   xml   file
public   void   parseXMLFile(Document   doc)throws   Exception{
//获得student元素的ID属性值
getStudentAttrValue(doc);
//获得student   的ID=101的子元素文本的内容
//getChildEleText(doc);
}
public   void   getStudentAttrValue(Document   doc){
NodeList   nodeLst=doc.getElementsByTagName( "student ");
int   len=nodeLst.getLength();
for(int   i=0;i <len;i++){
if(nodeLst.item(i)   instanceof   Element){
Element   eleStu=(Element)nodeLst.item(i);
Attr   attr=eleStu.getAttributeNode( "ID ");
System.out.println( "ID   value   is:   "+attr.getValue());
if(attr.getValue().equalsIgnoreCase( "101 ")){
getChildEleText(eleStu);
}
}
}

}
public   void   getChildEleText(Element   ele){
NodeList   childLst=ele.getChildNodes();
int   len=childLst.getLength();
for(int   j=0;j <len;j++){
if(childLst.item(j)   instanceof   Element){
Element   childEle=(Element)hildLst.item(j);
String   tagName=childEle.getTagName();
String   text=childEle.getTextContent();
System.out.println( " < "+tagName+ "> "+text+ " </ "+tagName+ "> ");
}
}
}
/**
  *   @param   args
  */
public   static   void   main(String[]   args)   {
ParseXMLByDom   parseXML=new   ParseXMLByDom();
try{
parseXML.getStudentAttrValue(parseXML.getDocumentByFile());
}catch(Exception   ex){
ex.printStackTrace();
}

}

}
 为什么String   text=childEle.getTextContent();报错呢??怎么解决啊??

------解决方案--------------------
有没有异常信息啊