DOM解析XML实现增删改查
wzh
18
男
lld
20
女
- 新建一个类DomParseXml
- init方法初始化dom
//创建DOM对象
private static Document init() throws Exception {
//借助字节输入流从磁盘获取xml文档到流中
FileInputStream fileInputStream = new FileInputStream("src/com/bosssoft/hr/train/xml/student.xml");
//得到DOM工厂实例对象
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//从工厂中获取DOM解析工具
DocumentBuilder documentBuilder = factory.newDocumentBuilder();
Document parse = documentBuilder.parse(fileInputStream);
return parse;
}
//保存xml
public static void saveXML(Document document,String path) throws TransformerException {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
transformerFactory.setAttribute("indent-number", 4);
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "YES");
transformer.transform(new DOMSource(document),new StreamResult(xmlPath+path));
}
//Dom解析,查询
public static void queryAll() throws Exception {
Document document = init();
NodeList studentList = document.getElementsByTagName("student");
//循环遍历
for (int i=0; i< studentList.getLength(); i++){
Node student = studentList.item(i);
if(student.getNodeType() == Node.ELEMENT_NODE){
NodeList childNodes = student.getChildNodes();
for(int j =0; j< childNodes.getLength(); j++){
Node child = childNodes.item(j);
if(child.getNodeType() == Node.ELEMENT_NODE){
String textContent = child.getTextContent();
String nodeName = child.getNodeName();
System.out.println(nodeName + "-----"+ textContent);
}
}
}
}
}
//通过id查询student 然后修改对应信息
public static void update(String id,String name,String path) throws Exception {
Document document = init();
NodeList StudentList = document.getElementsByTagName("student");
for(int i = 0; i< StudentList.getLength(); i++){
Node studentNode = StudentList.item(i);
Element stu = (Element) studentNode;
String id1 = stu.getAttribute("id");
if(id != null && id.equals(id1)){
NodeList childNodes = studentNode.getChildNodes();
for(int j = 0; j < childNodes.getLength(); j++){
String nodeName = childNodes.item(j).getNodeName();
if(nodeName.equals("name")){
childNodes.item(j).getFirstChild().setTextContent(name);
}
}
}
}
saveXML(document,path);
System.out.println("修改成功");
}
//添加
public static void add(String idvalue,String username ,String userage, String sex,String path) throws Exception {
Document document = init();
Element studentNode = document.createElement("student");
studentNode.setAttribute("id",idvalue);
Element nameNode = document.createElement("name");
Element ageNode = document.createElement("age");
Element sexNode = document.createElement("sex");
nameNode.appendChild(document.createTextNode(username));
ageNode.appendChild(document.createTextNode(userage));
sexNode.appendChild(document.createTextNode(sex));
studentNode.appendChild(nameNode);
studentNode.appendChild(ageNode);
studentNode.appendChild(sexNode);
document.getElementsByTagName("students").item(0).appendChild(studentNode);
saveXML(document,path);
System.out.println("添加用户成功");
}
//删除 通过id查询 指定用户删除
public static void delete(String id,String path) throws Exception {
Document document = init();
NodeList StudentList = document.getElementsByTagName("student");
for(int i = 0; i< StudentList.getLength(); i++){
Node studentNode = StudentList.item(i);
Element stu = (Element) studentNode;
String id1 = stu.getAttribute("id");
if(id != null && id.equals(id1)){
stu.getParentNode().removeChild(stu);
}
}
saveXML(document,path);
System.out.println("删除成功");
}
package com.bosssoft.hr.train.xml;
import org.w3c.dom.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.FileInputStream;
/**
* @作者 吴志鸿
* @maven 3.6.3
* @jdk 1.8
*/
public class DomParseXml {
private static String xmlPath = "src/com/bosssoft/hr/train/xml/";
//创建DOM对象
private static Document init() throws Exception {
//借助字节输入流从磁盘获取xml文档到流中
FileInputStream fileInputStream = new FileInputStream("src/com/bosssoft/hr/train/xml/student.xml");
//得到DOM工厂实例对象
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//从工厂中获取DOM解析工具
DocumentBuilder documentBuilder = factory.newDocumentBuilder();
Document parse = documentBuilder.parse(fileInputStream);
return parse;
}
//Dom解析,查询
public static void queryAll() throws Exception {
Document document = init();
NodeList studentList = document.getElementsByTagName("student");
//循环遍历
for (int i=0; i< studentList.getLength(); i++){
Node student = studentList.item(i);
if(student.getNodeType() == Node.ELEMENT_NODE){
NodeList childNodes = student.getChildNodes();
for(int j =0; j< childNodes.getLength(); j++){
Node child = childNodes.item(j);
if(child.getNodeType() == Node.ELEMENT_NODE){
String textContent = child.getTextContent();
String nodeName = child.getNodeName();
System.out.println(nodeName + "-----"+ textContent);
}
}
}
}
}
//保存xml
public static void saveXML(Document document,String path) throws TransformerException {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
transformerFactory.setAttribute("indent-number", 4);
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "YES");
transformer.transform(new DOMSource(document),new StreamResult(xmlPath+path));
}
//添加
public static void add(String idvalue,String username ,String userage, String sex,String path) throws Exception {
Document document = init();
Element studentNode = document.createElement("student");
studentNode.setAttribute("id",idvalue);
Element nameNode = document.createElement("name");
Element ageNode = document.createElement("age");
Element sexNode = document.createElement("sex");
nameNode.appendChild(document.createTextNode(username));
ageNode.appendChild(document.createTextNode(userage));
sexNode.appendChild(document.createTextNode(sex));
studentNode.appendChild(nameNode);
studentNode.appendChild(ageNode);
studentNode.appendChild(sexNode);
document.getElementsByTagName("students").item(0).appendChild(studentNode);
saveXML(document,path);
System.out.println("添加用户成功");
}
//通过id查询student 然后修改对应信息
public static void update(String id,String name,String path) throws Exception {
Document document = init();
NodeList StudentList = document.getElementsByTagName("student");
for(int i = 0; i< StudentList.getLength(); i++){
Node studentNode = StudentList.item(i);
Element stu = (Element) studentNode;
String id1 = stu.getAttribute("id");
if(id != null && id.equals(id1)){
NodeList childNodes = studentNode.getChildNodes();
for(int j = 0; j < childNodes.getLength(); j++){
String nodeName = childNodes.item(j).getNodeName();
if(nodeName.equals("name")){
childNodes.item(j).getFirstChild().setTextContent(name);
}
}
}
}
saveXML(document,path);
System.out.println("修改成功");
}
//删除 通过id查询 指定用户删除
public static void delete(String id,String path) throws Exception {
Document document = init();
NodeList StudentList = document.getElementsByTagName("student");
for(int i = 0; i< StudentList.getLength(); i++){
Node studentNode = StudentList.item(i);
Element stu = (Element) studentNode;
String id1 = stu.getAttribute("id");
if(id != null && id.equals(id1)){
stu.getParentNode().removeChild(stu);
}
}
saveXML(document,path);
System.out.println("删除成功");
}
}