目录:

  • 使用JAXP添加节点
  • 使用JAXP删除节点
  • 使用JAXP修改节点
  • 使用JAXP遍历节点

现有XML文件:

1.   使用jaxp添加节点

需求:在第一个<p1>内添加<sex>man</sex>
步骤:
(1)创建解析器工厂
(2)实例化解析器
(3)解析XML,得到Document
(4)得到第一个p1元素——firstNode
(5)创建新的Node,标签名称为sex
(6)创建新节点的文本
(7)将文本添加至新节点
(8)将新节点添加至p1元素——firstNode
(9)使用Stream流写回xml文本
具体代码如下:


package xml;
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 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 java.io.File;
import java.io.IOException;
public class XMLTest3 {
    public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException, TransformerException {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.parse("source" + File.separator + "person.xml");
        // 得到第一个p1
        Node firstNode = document.getElementsByTagName("p1").item(0);
        // 创建新的元素<sex>
        Element newElement = document.createElement("sex");
        // 创建文本
        Text text = document.createTextNode("man");
        // 将文本添加到<sex>元素下
        newElement.appendChild(text);
        // 将<sex>添加到第一个<p1>元素下
        firstNode.appendChild(newElement);
        // 写回xml文件
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.transform(new DOMSource(document), new StreamResult("source" + File.separator + "person.xml"));
    }
}

 

2.   使用jaxp删除节点

需求:删除<sex>man</sex>元素
步骤:
(1)创建解析器工厂
(2)实例化解析器
(3)解析XML,得到Document
(4)获取第一个sex标签
(5)获取sex标签的父标签——parentNode
(6)父标签parentNode删除子元素sex
(7)使用Stream流写回xml文本
代码如下:


package xml;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
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 java.io.File;
import java.io.IOException;
public class XMLTest4 {
    public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException, TransformerException {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.parse("source" + File.separator + "person.xml");
        // 获取第一个sex标签
        Node node = document.getElementsByTagName("sex").item(0);
        // 获取sex标签的父标签
        Node parentNode = node.getParentNode();
        // 父标签删除sex元素
        parentNode.removeChild(node);
        // 写回xml文件
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.transform(new DOMSource(document), new StreamResult("source" + File.separator + "person.xml"));
    }
}

 

3.   使用jaxp修改节点

需求:修改<sex>标签的文本值
步骤:
(1)创建解析器工厂
(2)实例化解析器
(3)解析XML,得到Document
(4)得到sex元素
(5)设置sex文本值
(6)使用Stream流写回xml文本
代码如下:


package xml;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.io.IOException;
public class XMLTest5 {
    public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException, TransformerException {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.parse("source" + File.separator + "person.xml");
        // 获取第一个sex标签
        Node node = document.getElementsByTagName("sex").item(0);
        // 设置sex标签的文本值
        node.setTextContent("woman");
        // 写回
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.transform(new DOMSource(document), new StreamResult("source" + File.separator + "person.xml"));
    }
}

 

3.   使用jaxp修改节点

需求:遍历所有的标签元素
步骤:
(1)创建解析器工厂
(2)实例化解析器
(3)解析XML,得到Document
使用递归
(4)得到根节点
(5)得到根节点的所有子节点
(6)判断得到的结点类型,如果是元素就打印
(6)递归调用
代码:


package xml;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
public class XMLTest6 {
    public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.parse("source" + File.separator + "person.xml");
        traverse(document);
    }
    public static void traverse(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++) {
            Node node1 = nodeList.item(i);
            traverse(node1);
        }
    }
}

 


代码编译器:IntelliJ IDEA 20.01
 


您必须 登录 才能发表评论