JAXP之DOM解析

JAXP之DOM解析

查询 修改 删除 遍历

package cn.itcast.jaxp.dom;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
 * /**
 * 使用JAXP的DOM方式解析XML 增删改查 先创建解析器工厂,用工厂生产出解析器,再用解析器解析XML文档
 *
 * @author Lynch
 *
 */
public class DOMTest {
    public static void main(String[] args) throws Exception {
        trasver();
    }
    /**
     * 查询某个节点
     *
     * @throws ParserConfigurationException
     * @throws IOException
     * @throws SAXException
     */
    public static void retrieve() throws ParserConfigurationException,
            IOException, Exception {
        // 创建解析器工厂
        DocumentBuilderFactory docbBuilderFactory = DocumentBuilderFactory
                .newInstance();
        // 生成解析器
        DocumentBuilder dBuilder = docbBuilderFactory.newDocumentBuilder();
        // 解析XML
        Document document = dBuilder.parse("src/book.xml");
        // 获得文档树的某个节点
        NodeList nodeList = document.getElementsByTagName("书名");
        // 获得节点
        Node node = nodeList.item(0);
        // 输出节点文本
        System.out.println(node.getTextContent());
    }
    /**
     * 添加元素
     *
     * @throws ParserConfigurationException
     * @throws IOException
     * @throws SAXException
     * @throws TransformerException
     */
    public static void addNode() throws ParserConfigurationException,
            SAXException, IOException, TransformerException {
        // 创建解析器工厂
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
                .newInstance();
        // 生成解析器
        DocumentBuilder documentBuilder = documentBuilderFactory
                .newDocumentBuilder();
        // 解析XML
        Document document = documentBuilder.parse("src/book.xml");
        // 先创建一个新的节点
        org.w3c.dom.Element newElement = document.createElement("作者");
        newElement.setTextContent("席慕容");
        // 找到要加节点的节点
        Node bookNode = document.getElementsByTagName("书").item(0);
        // 将新节点添加到节点中
        bookNode.appendChild(newElement);
        // 将内存中的文档树写到文件里面
        TransformerFactory transformerFactory = TransformerFactory
                .newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.transform(new DOMSource(document), new StreamResult(
                "src/book.xml"));
    }
    /**
     * DOM方式的修改
     *
     * @throws ParserConfigurationException
     * @throws IOException
     * @throws SAXException
     * @throws TransformerException
     */
    public static void modified() throws ParserConfigurationException,
            SAXException, IOException, TransformerException {
        // 解析器工厂
        DocumentBuilderFactory docFactory = DocumentBuilderFactory
                .newInstance();
        // 解析器
        DocumentBuilder dBuilder = docFactory.newDocumentBuilder();
        // 解析文档
        Document document = dBuilder.parse("src/book.xml");
        // 找到第一本书的售价节点
        Node price = document.getElementsByTagName("售价").item(0);
        // 修改文本
        price.setTextContent("999");
        // 回写文档
        TransformerFactory transformerFactory = TransformerFactory
                .newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.transform(new DOMSource(document), new StreamResult(
                "src/book.xml"));
    }
    /**
     * 删除一节点
     *
     * @throws ParserConfigurationException
     * @throws IOException
     * @throws SAXException
     * @throws TransformerException
     */
    public static void delete() throws ParserConfigurationException,
            SAXException, IOException, TransformerException {
        // 解析器工厂
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
                .newInstance();
        // 解析器
        DocumentBuilder dBuilder = documentBuilderFactory.newDocumentBuilder();
        // 解析文档
        Document document = dBuilder.parse("src/book.xml");
        // 找到节点
        Node author = document.getElementsByTagName("作者").item(1);
        // 删除节点
        author.getParentNode().removeChild(author);
        // 回写文档
        TransformerFactory transformerFactory = TransformerFactory
                .newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.transform(new DOMSource(document), new StreamResult(
                "src/book.xml"));
    }
    /**
     * 遍历所有节点
     *
     * @throws ParserConfigurationException
     * @throws IOException
     * @throws SAXException
     */
    public static void trasver() throws ParserConfigurationException,
            SAXException, IOException {
        // 解析器工厂
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
                .newInstance();
        // 解析器
        DocumentBuilder documentBuilder = documentBuilderFactory
                .newDocumentBuilder();
        // 解析文档
        Document document = documentBuilder.parse("src/book.xml");
        // 遍历
        list(document);
    }
    /**
     * 遍历辅助函数
     * @param node
     */
    public static void list(Node node) {
        if (node instanceof Element) {
            System.out.println(node.getNodeName());
        }
        NodeList nodeList = node.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node rootNode = nodeList.item(i);
            list(rootNode);
        }
    }
}


本文出自 “狐灵传说” 博客,转载请与作者联系!

你可能感兴趣的:(import,dom解析)