本文通过使用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()
//getSingleNameValue()
//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
{
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
{
Node name = names.item(i)
String namevalue = name.getTextContent()
System.out.println(namevalue)
}
}
}