java用DOM方法解析xml文档

步骤:

1、创建DocumentBuliderFactory对象

DocumentBuilder documentBuilder=documentBuilderFactory.newDocumentBuilder();

2、创建DocumentBuilder对象(方法会抛出异常)

DocumentBuilder documentBuilder=documentBuilderFactory.newDocumentBuilder();

3、创建Document对象,用parse方法加载要解析的xml文件

Document document=documentBuilder.parse("book.xml");

这样DOM解析xml文档的准备工作就完成啦

下面是一些常用的节点类型的介绍



DOM操作中常用的对象:

1、节点对象:Node(包含元素节点,属性节点,文本节点)

getAttributes():获取节点属性值的map集合,返回NamedNodeMap对象

getNopeType():获取节点类型

getNodeName():返回值如上图所示

getNodeValue():返回值如上图所示

getChildNodes():获取节点的所有子节点,返回NodeList对象

getFirstChild():获取第一个子节点

getLastChild():获取最后一个子节点

getTextContent():获取节点中的所有文本内容

2、节点集合对象:NodeList

getLength():获取集合长度

item():根据传入的索引值,获取索引值所指向的节点

3、元素节点对象:Element

实例源码:

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
    <book id="1">
        <name>三国演义</name>
        <author>罗贯中</author>
        <year>元末明初</year>
    </book>
    <book id="2">
        <name>红楼梦</name>
        <author>曹雪芹</author>
        <year>明代</year>
    </book>
import org.w3c.dom.*;
import org.xml.sax.SAXException;


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;

public class XMLDom {
    public static void main(String[] args){
        //创建DocumentBuilderFactory对象
        DocumentBuilderFactory documentBuilderFactory=DocumentBuilderFactory.newInstance();
        try {
            //创建DocumentBuilder对象
            DocumentBuilder documentBuilder=documentBuilderFactory.newDocumentBuilder();
            //创建Document对象
            Document document=documentBuilder.parse("book.xml");
            //用DOM操作中getElementByTagName方法获取“book”标签的集合
            NodeList books=document.getElementsByTagName("book");
            //第一层循环获取book的id属性
            for(int i=0;i<books.getLength();i++){
                //获取单个book节点
                Element book=(Element) books.item(i);
                String id=book.getAttribute("id");
                System.out.println("第"+(i+1)+"本书的属性值为"+id);
                //获取book标签中的所有子节点
                NodeList childNode=book.getChildNodes();
                //第二层循环获取book中子节点标签名和文本内容
                for(int j=0;j<childNode.getLength();j++){
                    //获取单个子节点
                    Node child=childNode.item(j);
                    //获取子节点的节点名
                    String name=child.getNodeName();
                    //获取节点中的文本内容
                    String value=child.getTextContent();
                    //如果所得字符串经过trim方法后为不为空字符串再打印输出
                    if(!value.trim().equals(""))
                    System.out.println(name+"的值为:"+value);
                }
            }
        } catch (ParserConfigurationException | SAXException | IOException e) {
            e.printStackTrace();
        }
    }
}


关注公众号,和我一起学习




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值