dom解析xml疑问
<?xmlversion="1.0"encoding="UTF-8"?><publication><book><title>themythicalman-month</t...
<?xml version="1.0" encoding="UTF-8"?>
<publication>
<book>
<title>the mythical man-month</title>
<writer>frederick p.brooks Jr.</writer>
<publishdate>1975-03-12</publishdate>
</book>
<book>
<title>think in java</title>
<writer>bruce eckel</writer>
<publishdate>1999-11-01</publishdate>
</book>
</publication>
代码如下
public class useDomPrintElement {
public static void main(String[] args){
try{
//获得一个XML文件的解析器
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
//解析XML文件生成DOM文档的接口类,以便访问DOM
DocumentBuilder builder=factory.newDocumentBuilder();
//Document接口描述了对应于整个XML文件的文档树
Document document=builder.parse(new File("publication.xml"));
//获得根元素的子节点列表
Element element=document.getDocumentElement();
NodeList nodelist=element.getChildNodes();
//如果预知根元素的名称,也可以使用下边的方法获取根元素的子节点列表
//NodeList nodelist=document.getElementsByTagName("publication");
//用递归方法实现DOM文档的遍历
GetElement(nodelist);
}catch(Exception e){
e.printStackTrace();
}
}
public static void GetElement (NodeList nodelist){
Node cnode;
String str;
if(nodelist.getLength()==0){
//该节点没有子节点
return;
}
for(int i=0;i<nodelist.getLength();i++){
cnode=nodelist.item(i);
System.out.println("nodelist:"+nodelist);
//System.out.println("getlength:"+nodelist.getLength());
if(cnode.getNodeType()==Node.ELEMENT_NODE){
//打印节点名称
//System.out.println(cnode.getNodeName()+":");
//递归调用GetElement
GetElement(cnode.getChildNodes());
}
else if(cnode.getNodeType()==Node.TEXT_NODE){
str=cnode.getNodeValue().trim();
//当该对象不为空时显示信息
//if(str.length()>0)
// System.out.println(" "+str);
}
}
}
}
打印结果
nodelist:[publication: null]
nodelist:[publication: null]
nodelist:[book: null]
nodelist:[book: null]
nodelist:[title: null]
nodelist:[book: null]
nodelist:[book: null]
nodelist:[writer: null]
nodelist:[book: null]
nodelist:[book: null]
nodelist:[publishdate: null]
nodelist:[book: null]
nodelist:[publication: null]
nodelist:[publication: null]
nodelist:[book: null]
nodelist:[book: null]
nodelist:[title: null]
nodelist:[book: null]
nodelist:[book: null]
nodelist:[writer: null]
nodelist:[book: null]
nodelist:[book: null]
nodelist:[publishdate: null]
nodelist:[book: null]
nodelist:[publication: null]
打印的结果不明白,还有nodelist长度是怎么算的 展开
<publication>
<book>
<title>the mythical man-month</title>
<writer>frederick p.brooks Jr.</writer>
<publishdate>1975-03-12</publishdate>
</book>
<book>
<title>think in java</title>
<writer>bruce eckel</writer>
<publishdate>1999-11-01</publishdate>
</book>
</publication>
代码如下
public class useDomPrintElement {
public static void main(String[] args){
try{
//获得一个XML文件的解析器
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
//解析XML文件生成DOM文档的接口类,以便访问DOM
DocumentBuilder builder=factory.newDocumentBuilder();
//Document接口描述了对应于整个XML文件的文档树
Document document=builder.parse(new File("publication.xml"));
//获得根元素的子节点列表
Element element=document.getDocumentElement();
NodeList nodelist=element.getChildNodes();
//如果预知根元素的名称,也可以使用下边的方法获取根元素的子节点列表
//NodeList nodelist=document.getElementsByTagName("publication");
//用递归方法实现DOM文档的遍历
GetElement(nodelist);
}catch(Exception e){
e.printStackTrace();
}
}
public static void GetElement (NodeList nodelist){
Node cnode;
String str;
if(nodelist.getLength()==0){
//该节点没有子节点
return;
}
for(int i=0;i<nodelist.getLength();i++){
cnode=nodelist.item(i);
System.out.println("nodelist:"+nodelist);
//System.out.println("getlength:"+nodelist.getLength());
if(cnode.getNodeType()==Node.ELEMENT_NODE){
//打印节点名称
//System.out.println(cnode.getNodeName()+":");
//递归调用GetElement
GetElement(cnode.getChildNodes());
}
else if(cnode.getNodeType()==Node.TEXT_NODE){
str=cnode.getNodeValue().trim();
//当该对象不为空时显示信息
//if(str.length()>0)
// System.out.println(" "+str);
}
}
}
}
打印结果
nodelist:[publication: null]
nodelist:[publication: null]
nodelist:[book: null]
nodelist:[book: null]
nodelist:[title: null]
nodelist:[book: null]
nodelist:[book: null]
nodelist:[writer: null]
nodelist:[book: null]
nodelist:[book: null]
nodelist:[publishdate: null]
nodelist:[book: null]
nodelist:[publication: null]
nodelist:[publication: null]
nodelist:[book: null]
nodelist:[book: null]
nodelist:[title: null]
nodelist:[book: null]
nodelist:[book: null]
nodelist:[writer: null]
nodelist:[book: null]
nodelist:[book: null]
nodelist:[publishdate: null]
nodelist:[book: null]
nodelist:[publication: null]
打印的结果不明白,还有nodelist长度是怎么算的 展开
展开全部
DOM方式访问XML,在xml文件中的回车换行符,Dom会认为是一个纯文本
你可将xml文档中的所有换行去掉,看一下结果,你就明白了
如:
<?xml version="1.0" encoding="UTF-8"?><publication><book><title>the mythical man-month</title><writer>frederick p.brooks Jr.</writer>
<publishdate>1975-03-12</publishdate></book><book><title>think in java</title><writer>bruce eckel</writer><publishdate>1999-11-01</publishdate></book></publication>
看看结果
你可将xml文档中的所有换行去掉,看一下结果,你就明白了
如:
<?xml version="1.0" encoding="UTF-8"?><publication><book><title>the mythical man-month</title><writer>frederick p.brooks Jr.</writer>
<publishdate>1975-03-12</publishdate></book><book><title>think in java</title><writer>bruce eckel</writer><publishdate>1999-11-01</publishdate></book></publication>
看看结果
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询