xml解析器jaxp的使用

Posted linghuchenchen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了xml解析器jaxp的使用相关的知识,希望对你有一定的参考价值。

jaxp是javase的一部分,就是jdk中的一部分
javax.xml.parsers包
DocumentBuilder
DocumentBuilderFactory
SAXParser
SAXParserFactory

1、添加节点
    //1、创建解析器工厂
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    //2、创建解析器
    DocumentBuilder builder = builderFactory.newDocumentBuilder();
    //3、解析xml,返回document
    Document doc = builder.parse("./src/main/resources/persons.xml");
    //4、得到第一个person标签
    Node person = doc.getElementsByTagName("person").item(0);
    //5、创建sex标签 createElement
    Element sex = doc.createElement("sex");
    //6、创建文本 createTextNode
    Text text = doc.createTextNode("男");
    //7、把文本添加到sex标签下面 appendChild
    sex.appendChild(text);
    //8、把sex标签添加到第一个person下面
    person.appendChild(sex);
    //9、回写
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.transform(new DOMSource(doc),new StreamResult("./src/main/resources/persons.xml"));

2、修改节点
    //1、创建解析器工厂
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    //2、创建解析器
    DocumentBuilder builder = builderFactory.newDocumentBuilder();
    //3、解析xml,返回document
    Document doc = builder.parse("./src/main/resources/persons.xml");
    //4、得到第一个person标签
    Node sex = doc.getElementsByTagName("sex").item(0);
    //5、修改sex标签 setTextContext()
    sex.setTextContent("女");
    //6、回写
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.transform(new DOMSource(doc),new StreamResult("./src/main/resources/persons.xml"));
3、删除节点
    //1、创建解析器工厂
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    //2、创建解析器
    DocumentBuilder builder = builderFactory.newDocumentBuilder();
    //3、解析xml,返回document
    Document doc = builder.parse("./src/main/resources/persons.xml");
    //4、得到第一个sex标签
    Node sex = doc.getElementsByTagName("sex").item(0);
    //5、获取sex的父节点
    Node person = sex.getParentNode();
    //6、使用父节点删除 removeChild()
    person.removeChild(sex);
    //7、回写
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.transform(new DOMSource(doc),new StreamResult("./src/main/resources/persons.xml"));
4、遍历节点
    public static void queryList() throws ParserConfigurationException, IOException, SAXException {
        //1、创建解析器工厂
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

        //2、创建解析器
        DocumentBuilder builder = builderFactory.newDocumentBuilder();

        //3、解析xml返回document
        Document doc = builder.parse("./src/main/resources/persons.xml");

        //4、递归遍历
        RecursiveNodeList(doc);
    }

    private static void RecursiveNodeList(Node node) {
        //判断是元素类型才打印
        if (node.getNodeType()==Node.ELEMENT_NODE){
            System.out.println(node.getNodeName());
        }

        //得到当前节点的子节点集合
        NodeList nodeList = node.getChildNodes();

        //遍历
        for (int i= 0;i<nodeList.getLength();i++){
            RecursiveNodeList(nodeList.item(i));
        }
    }

 

以上是关于xml解析器jaxp的使用的主要内容,如果未能解决你的问题,请参考以下文章

xml解析-jaxp添加结点

xml解析-jaxp修改结点

xml解析-jaxp删除结点

使用 XSD、目录解析器和用于 XSLT 的 JAXP DOM 验证 XML

XML解析——jaxp

XML解析器之JAXP与DOM4J