使用jaxp对xml文件进行增删改查以及遍历操作

时间:2022-10-20 22:13:15

本文通过使用jaxp的方式来实现对于xml文件的增删改查操作,以最基础和简单的来做为实例,所操作的xml文件为person.xml,代码如下:

<?xml version="1.0" encoding="UTF-8" standalone="no"?><person>
<p1>
<name>nike</name>
<age>23</age>
</p1>
<p1>
<name>tom</name>
<age>25</age>
</p1>
</person>

因为方便的关系,这里将这些操作放在同一个java文件中,通过函数的方式进行调用,代码如下:


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
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.w3c.dom.Text;

public class Textjaxp {

public static void main(String[] args) throws Exception {

//getNameValue(); //获取xml中标的签值
//getSingleNameValue(); //获取xml中某个标签值
//addNode(); //添加节点
//removeNode(); //修改节点
//deleteNode(); //删除节点
showAllNodeName(); //遍历所有节点,使用递归的思想

}



private static void showAllNodeName() throws Exception {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
//创建Xml解析器工厂

DocumentBuilder builder = builderFactory.newDocumentBuilder();
//创建Xml解析器
Document doc = builder.parse("src/person.xml");
//解析xml返回document
show(doc);

}



private static void show(Node node) {
if(node.getNodeType()==Node.ELEMENT_NODE){
System.out.println(node.getNodeName());
}
//判断是否为节点
NodeList list = node.getChildNodes();

for(int i=0;i<list.getLength();i++)
{
Node node1 = list.item(i);

show(node1);//递归调用


}

}




private static void deleteNode() throws Exception {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

DocumentBuilder builder = builderFactory.newDocumentBuilder();

Document doc = builder.parse("src/person.xml");

NodeList list = doc.getElementsByTagName("sex");

Node sex = list.item(0);

Node parent = sex.getParentNode();

parent.removeChild(sex);
//xml回写(只要xml文件有改动都需要回写操作,否则结果不会改动xml文件)
TransformerFactory transformerFactory = TransformerFactory.newInstance();

Transformer transformer = transformerFactory.newTransformer();

transformer.transform(new DOMSource(doc), new StreamResult("src/person.xml"));
}




private static void removeNode() throws Exception {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

DocumentBuilder builder = builderFactory.newDocumentBuilder();

Document doc = builder.parse("src/person.xml");

NodeList list = doc.getElementsByTagName("sex");

Node sex1 = list.item(0);

sex1.setTextContent("nan");

TransformerFactory transformerFactory = TransformerFactory.newInstance();

Transformer transformer = transformerFactory.newTransformer();

transformer.transform(new DOMSource(doc), new StreamResult("src/person.xml"));
}




private static void addNode() throws Exception {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

DocumentBuilder builder = builderFactory.newDocumentBuilder();

Document document = builder.parse("src/person.xml");

NodeList list = document.getElementsByTagName("p1");

Node p1 = list.item(0);

Element sex1 = document.createElement("sex");

Text text1 = document.createTextNode("nv");

sex1.appendChild(text1);

p1.appendChild(sex1);

TransformerFactory transformerFactory = TransformerFactory.newInstance();

Transformer transformer = transformerFactory.newTransformer();

transformer.transform(new DOMSource(document), new StreamResult("src/person.xml"));

}




private static void getSingleNameValue() throws Exception {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

DocumentBuilder builder = builderFactory.newDocumentBuilder();

Document doc = builder.parse("src/person.xml");

NodeList list = doc.getElementsByTagName("name");

Node name = list.item(0);

String nameValue = name.getTextContent();

System.out.println(nameValue);
}




private static void getNameValue() throws Exception {
// TODO Auto-generated method stub
DocumentBuilderFactory buliBuilderFactory = DocumentBuilderFactory.newInstance();

DocumentBuilder builder = buliBuilderFactory.newDocumentBuilder();

Document document = builder.parse("src/person.xml");

NodeList names = document.getElementsByTagName("name");

for(int i =0;i<names.getLength();i++)
{
Node name = names.item(i);

String namevalue = name.getTextContent();

System.out.println(namevalue);
}
}

}