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());
}
}
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());
}
}
相关文章推荐
- php遍历所有文件及文件夹的方法深入解析
- 解析xml文件,遍历输出xml文件中的所有节点, 最终模仿实现struts2框架
- 解析xml文件,遍历输出xml文件中的所有节点, 最终模仿实现struts2框架
- 遍历某目录下的所有文件的方法
- jQuery基础教程之DOM操作-遍历节点-find()方法
- Dom遍历所有的节点 read
- PHP 遍历目录下的所有文件的方法
- 遍历目录下所有图片文件显示的方法.(已废弃)
- 遍历课上例题shuiguo.xml文件中根节点的所有节点,打印节点名称和节点值
- jQuery基础教程之DOM操作-遍历节点-prevUntil()方法
- 遍历文件夹中所有文件的方法
- Asp.net遍历ini配置文件节点下所有key和value
- 遍历dom文档的所有节点
- jQuery基础教程之DOM操作-遍历节点-closest()方法
- jQuery基础教程之DOM操作-遍历节点-next()方法
- javascript遍历DOM节点方法详解
- PHP 遍历目录下的所有文件的方法
- javascript递归遍历所有DOM文档节点
- 使用DCMTK遍历读取DICOM文件所有Tag的方法