您的位置:首页 > 其它

Dom解析文件,遍历节点,不是所有节点,方法

2014-10-11 10:08 453 查看
package jaxp.dom;

import java.io.File;

import java.io.IOException;

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.NamedNodeMap;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;

import org.xml.sax.SAXException;

/**

* Dom解析文件,遍历节点,不是所有节点,方法一

* @author Administrator

*

*/

public class DomXML_1 {

public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {

//第1创建DocumentBuilderFactory对象

DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();

// 第2步创建DocumentBuilder对象

DocumentBuilder db=dbf.newDocumentBuilder();

// 第3步利用 DocumentBuilder.parse的方法把对booksl.xml的处理转化成对Document的处理

Document doc=db.parse(new File("src//booksl.xml"));

//获取子节点的方法

Element elementRoot=doc.getDocumentElement();//获取根节点

// 节点名称books节点类型1节点值null

System.out.println("节点名称"+elementRoot.getNodeName()+"节点类型"+elementRoot.getNamespaceURI()+"节点值"+elementRoot.getNodeValue());

// 可以去获取它的孩子节点

// 首先判断他是否有孩子节点

if(elementRoot.hasChildNodes()){

// 怎么获取

NodeList list=elementRoot.getChildNodes();

System.out.println("孩子节点的个数"+list.getLength());

// 遍历

for(int index=0;index<list.getLength();index++){

Node node=list.item(index);

NamedNodeMap nnmap=node.getAttributes();

if(nnmap!=null){

for(int j=0;j<nnmap.getLength();j++){

Node n=nnmap.item(j);

System.out.println("-节点名称"+n.getNodeName()+"—节点类型"+n.getNodeType()+"—节点值"+n.getNodeValue());

}

}

}

}else{

System.out.println("mei");

}

}

}

package jaxp.dom;

import java.io.File;

import java.io.IOException;

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.NamedNodeMap;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;

import org.xml.sax.SAXException;

/**

* 用Dom解析文件,遍历根节点下的所有节点,方法2

* @author Administrator

*

*/

public class DomXML_2 {

public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {

// 第1创建DocumentBuilderFactory对象

DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();

// 第2步创建DocumentBuilder对象

DocumentBuilder db=dbf.newDocumentBuilder();

// 第3步利用 DocumentBuilder.parse的方法把对booksl.xml的处理转化成对Document的处理

Document doc=db.parse(new File("src//booksl.xml"));

// 获取根元素节点

Element root=doc.getDocumentElement();

new DomXML_2().listNodes(root);

}

private void listNodes(Node node) {

// 判断是否有孩子节点

if(node.hasChildNodes()){

System.out.println("--节点名称"+node.getNodeName()+"--节点类型"+node.getNodeType()+"--节点值"+node.getNodeValue());

// 判断是否有属性

if(node.hasAttributes()){

// 就获取所有的属性节点

NamedNodeMap nnmap= node.getAttributes();

// 遍历属性节点

for(int indexj=0;indexj<nnmap.getLength();indexj++){

// 获取具体的属性节点对像

Node attr=nnmap.item(indexj);

System.out.println("--节点名称"+attr.getNodeName()+"--节点类型"+attr.getNodeType()+"--节点值"+attr.getNodeValue());

}

}

// 获取当前节点下面的所有孩子节点

NodeList childNodes=node.getChildNodes();

// 遍历孩子节点

for(int index=0;index<childNodes.getLength();index++){

// 获取具体的某个孩子节点(这里获取的孩子节点可能是文本节点,元素节点,属性节点,所以把上面的Element 换成Node)

Node childNode=childNodes.item(index);

// 遍历去

listNodes(childNode);

}

}else

System.out.println("--节点名称"+node.getNodeName()+"--节点类型"+node.getNodeType()+"--节点值"+node.getNodeValue());

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: