递归实现DOM解析任何XML文档

<?xml version="1.0" encoding="UTF-8"?>
<学生名册>
    <学生 学号="1">
        <姓名>张三</姓名>
        <性别>男</性别>
        <年龄>20</年龄>
    </学生>
    <学生 学号="2">
        <姓名>李四</姓名>
        <性别>女</性别>
        <年龄>19</年龄>
    </学生>
    <学生 学号="3">
        <姓名>王五</姓名>
        <性别>男</性别>
        <年龄>21</年龄>
    </学生>
</学生名册>

上面是XML


import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Attr;
import org.w3c.dom.Comment;
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;


public class DOM_XML_1 {

    /**
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        //获得解析工厂
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        //获得解析器
        DocumentBuilder db = dbf.newDocumentBuilder();
        //获得根元素节点
        Document doc = db.parse(new File("student.xml"));
        //得到根元素
        Element root = doc.getDocumentElement();
        
        parseElement(root);
    }
    //用递归实现解析,并给与输出
    private static void parseElement(Element element){
        //获得传进来的元素的名字
        String tagName = element.getNodeName();
        //获得子节点
        NodeList children = element.getChildNodes();
        
        System.out.print("<" + tagName);
        //获得属性
        NamedNodeMap map = element.getAttributes();
        //判断是否有属性
        if(null!=map){
            //循环取出属性的名字和值
            for(int i = 0;i<map.getLength();i++){
                
                Attr attr = (Attr)map.item(i);
                
                String attrName = attr.getName();
                String attrValue = attr.getValue();
                
                System.out.print("   " + attrName + "=\"" + attrValue +"\"");
            }
        }
        System.out.print(">");
        //循环遍历子节点
        for(int i = 0;i<children.getLength();i++){
            //取得子节点
            Node node = children.item(i);
            //获得字节点得类型
            short nodeType = node.getNodeType();
            //判断是否是元素
            if(nodeType ==Node.ELEMENT_NODE){
                //实现递归
                parseElement((Element)node);
            }
            //判断是否是文本
            else if(nodeType ==Node.TEXT_NODE){
                
                System.out.print(node.getNodeValue());
            }
            //判断是否是属性
            else if(nodeType==Node.COMMENT_NODE){
                
                System.out.print("<!--");
                Comment comment = (Comment)node;
                //获得属性的值
                String data = comment.getData();
                
                System.out.print(data);
                System.out.print("-->");
            }
        }
        
        System.out.print("</" +tagName +">");
    }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值