用java操作jaxp解析器,对xml实现增删改查

1.在src目录下建立person.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<person>
	<p1>
		<name>xiaoming</name>
		<age>20</age>
	</p1>
	<p1>
		<name>xiaofang</name>
		<age>22</age>
	</p1>
</person>

2.创建一个Java类

import java.io.IOException;

import javax.xml.crypto.dsig.Transform;
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 org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;

public class JaxpText {

	public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, TransformerException {
		//selectAll();
		//addSex();
		//modifySex();
		//delSex();
		listElement();
	}
	//遍历节点,并打印出来
	public static void listElement() throws ParserConfigurationException, SAXException, IOException{
		/**
		 * 1.创建解析器工厂
		 * 2.根据解析器工厂创建解析器
		 * 3.解析xml,返回document
		 * 
		 * ***递归可实现***
		 * 4.得到根节点
		 * 5.得到根节点的子节点
		 * 6.得到根节点的子节点的子节点
		 */
		//1.创建解析器工厂
		DocumentBuilderFactory builderFactor = DocumentBuilderFactory.newInstance();
		//2.根据解析器工厂创建解析器
		DocumentBuilder builder = builderFactor.newDocumentBuilder();
		//3.解析xml,返回document
		Document document = builder.parse("src/person.xml");
		
		//实现遍历的方法
		list1(document);
	}
	private static void list1(Node node) {
		// TODO Auto-generated method stub
		//判断是元素类型才打印
		if(node.getNodeType() == Node.ELEMENT_NODE){
			System.out.println(node.getNodeName());
		}
		//得到一层子节点
		NodeList list = node.getChildNodes();
		//遍历list
		for (int i = 0; i < list.getLength(); i++) {
			//得到每一个子节点
			Node node1 = list.item(i);
			//继续得到node1的子节点
			//node1.getChildNodes();
			//递归
			list1(node1);
		}
	}
	//删除sex标签
	public static void delSex() throws ParserConfigurationException, SAXException, IOException, TransformerException{
		/**
		 * 1.创建解析器工厂
		 * 2.根据解析器工厂创建解析器
		 * 3.解析xml,返回document
		 * 4.得到sex标签,item方法
		 * 5.用getParentNode方法获得sex的父节点
		 * 6.用removeChild方法删除子节点
		 * 7.回写xml
		 */
		//1.创建解析器工厂
		DocumentBuilderFactory builderFactor = DocumentBuilderFactory.newInstance();
		//2.根据解析器工厂创建解析器
		DocumentBuilder builder = builderFactor.newDocumentBuilder();
		//3.解析xml,返回document
		Document document = builder.parse("src/person.xml");
		//4.得到sex标签,item方法
		NodeList list = document.getElementsByTagName("sex");
		Node sex1 = list.item(0);
		//5.用getParentNode方法获得sex的父节点
		Node p1 =sex1.getParentNode();
		//6.用removeChild方法删除子节点
		p1.removeChild(sex1);
		//7.回写xml
		TransformerFactory transformerFactory = TransformerFactory.newInstance();
		Transformer transformer = transformerFactory.newTransformer();
		transformer.transform(new DOMSource(document), new StreamResult("src/person.xml"));
	}
	
	//修改sex标签
	public static void modifySex() throws ParserConfigurationException, SAXException, IOException, TransformerException{
		/**
		 * 1.创建解析器工厂
		 * 2.根据解析器工厂创建解析器
		 * 3.解析xml,返回document
		 * 4.得到sex标签,item方法
		 * 5.用setTextContoent修改sex的值
		 * 6.回写xml
		 */
		//1.创建解析器工厂
		DocumentBuilderFactory builderFactor = DocumentBuilderFactory.newInstance();
		//2.根据解析器工厂创建解析器
		DocumentBuilder builder = builderFactor.newDocumentBuilder();
		//3.解析xml,返回document
		Document document = builder.parse("src/person.xml");
		//4.得到sex标签,item方法
		NodeList list = document.getElementsByTagName("sex");
		Node node1 = list.item(0);
		//5.用setTextContoent修改sex的值
		node1.setTextContent("nan");
		//6.回写xml
		TransformerFactory transformerFactory = TransformerFactory.newInstance();
		Transformer transformer = transformerFactory.newTransformer();
		transformer.transform(new DOMSource(document), new StreamResult("src/person.xml"));
	}

	//添加sex标签
	public static void addSex() throws ParserConfigurationException, SAXException, IOException, TransformerException{
	   /**
	    *1.创建解析器工厂
		*2.根据解析器工厂创建解析器
		*3.解析xml,返回document
		*4.得到第一个标签P1,用item方法的得
		*5.用createElement方法创建sex标签
		*6.用createTextNode创建文本
		*7.用appendChild把文本添加到标签下
		*8.把sex添加到P1下
		*9.回写xml
		*/
		//1.创建解析器工厂
		DocumentBuilderFactory buliderFactor = DocumentBuilderFactory.newInstance();
		//2.根据解析器工厂创建解析器
		DocumentBuilder bulider = buliderFactor.newDocumentBuilder();
		//3.解析xml,返回document
		Document document = bulider.parse("src/person.xml");
		//4.得到第一个标签P1,用item方法的得
		NodeList list = document.getElementsByTagName("p1");
		Node node1 = list.item(0);
		//5.用createElement方法创建sex标签
		Element sex1 = 	document.createElement("sex");
		//6.用createTextNode创建文本
		Text text1 =document.createTextNode("nv");
		//7.用appendChild把文本添加到标签下
		sex1.appendChild(text1);
		//8.把sex添加到P1下
		node1.appendChild(sex1);
		//9.回写xml
		TransformerFactory transformerFactory = TransformerFactory.newInstance();
		Transformer transformer = transformerFactory.newTransformer();
		transformer.transform(new DOMSource(document), new StreamResult("src/person.xml"));
		
		
	
	}
	
	//查询标签
	private static void selectAll() throws ParserConfigurationException, SAXException, IOException {
		//1.创建解析工厂
		DocumentBuilderFactory builderFactor = DocumentBuilderFactory.newInstance();
		//2.根据解析工厂创建解析器
		DocumentBuilder builder = builderFactor.newDocumentBuilder();
		//解析xml,返回document
		Document document = builder.parse("src/person.xml");
		//得到所有查询到的元素
		NodeList list = document.getElementsByTagName("age");
		//遍历集合,得到每一个元素的值
		for (int i = 0; i < list.getLength(); i++) {
			Node name1 = list.item(i);
			String content =name1.getTextContent();
			System.out.println(content);
		}
	}

}

发布了22 篇原创文章 · 获赞 5 · 访问量 1798
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 创作都市 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览