码迷,mamicode.com
首页 > 其他好文 > 详细

xml解析器jaxp的使用

时间:2020-06-26 12:36:43      阅读:62      评论:0      收藏:0      [点我收藏+]

标签:str   tran   ==   one   工厂   类型   document   文本   遍历   

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的使用

标签:str   tran   ==   one   工厂   类型   document   文本   遍历   

原文地址:https://www.cnblogs.com/linghuchenchen/p/13194222.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!