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();报错呢??怎么解决啊??
/**
* 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();报错呢??怎么解决啊??
解决方案 »
- 网络编程ip问题
- 匿名内部类是否可以创建构造函数
- Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
- wait()与用一个for循环耗时有什么区别?
- log如何使用
- 如何自定义系统边框?
- java里使用canvas画图的问题——高手帮忙啊!
- 关于Canvas盖住JMenuItem的问题!!!
- [高人请进]关于自建class的问题,急在线等待
- 为什么我在JApplet下执行SQL就出错.
- java求1000之内完数,我写好了,可是没有任何输出结果,谁能帮我看一下,谢谢啦
- java socket传文件丢失数据???
能不能具体讲下怎么调整!!
试试吧
现将J2EE remove,然后编译,再将j2ee加上就可以了