jaxp对xml解析

得到代表文档的document对象

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;




public class demo  {
	public static void main(String[] args) throws Exception {
		//创建工厂
		DocumentBuilderFactory builderFactory=DocumentBuilderFactory.newInstance();
		//解析器
		DocumentBuilder builder= builderFactory.newDocumentBuilder();
		//document对象
		Document document= builder.parse("day01/book.xml");
	}
	

}

增删该查

import java.io.FileOutputStream;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


public class demo2  {
	//利用递归遍历每一个节点

	public  void read()throws Exception {
		
		DocumentBuilderFactory builderFactory= DocumentBuilderFactory.newInstance();
		DocumentBuilder builder=builderFactory.newDocumentBuilder();
		Document document=builder.parse("day01/book.xml");
		
		Node root=document.getElementsByTagName("书架").item(0);
		list(root);
		
	}

	private  void list(Node root) {
		// TODO Auto-generated method stub
		System.out.println(root.getNodeName());
		//得到所有儿子节点
		NodeList list=root.getChildNodes();
		for(int i=0;i<list.getLength();i++)
		{
			//每一个儿子节点
			if(list.item(i)instanceof Element)
			{
				System.out.println(list.item(i));
				list(list.item(i));
			}
			
		}
		
	}
	//
	
	public void add() throws Exception
	{
		DocumentBuilderFactory builderFactory=DocumentBuilderFactory.newInstance();
		DocumentBuilder builder=builderFactory.newDocumentBuilder();
		Document document=builder.parse("day01/book.xml");
		
		//创建节点
		Node node=document.createElement("售价");
		node.setTextContent("100");
		Node bookName=document.getElementsByTagName("书名").item(0);
		//增加节点
		bookName.getParentNode().appendChild(node);
		
		//更新
		TransformerFactory factory=TransformerFactory.newInstance();
		Transformer transformer=factory.newTransformer();
		transformer.transform(new DOMSource(document), new StreamResult(new FileOutputStream("day01/book.xml")));
		
		
	}
	//增加指定位置的节点,增加两步
	
	public void add2() throws Exception
	{
		DocumentBuilderFactory builderFactory=DocumentBuilderFactory.newInstance();
		DocumentBuilder builder=builderFactory.newDocumentBuilder();
		Document document=builder.parse("day01/book.xml");
		
		//参考节点
		Node canNode=document.getElementsByTagName("作者").item(0);
		
		Node node=document.createElement("售价");
		node.setTextContent("100");
		Node bookName=document.getElementsByTagName("书").item(0);
		bookName.getParentNode().appendChild(node);
		//调用inserBefore
		bookName.insertBefore(node, canNode);
		
		//
		TransformerFactory factory=TransformerFactory.newInstance();
		Transformer transformer=factory.newTransformer();
		transformer.transform(new DOMSource(document), new StreamResult(new FileOutputStream("day01/book.xml")));
		
		
	}
	//增加属性,得到属性
	@Test
	public void addAttribute() throws Exception
	{
		DocumentBuilderFactory builderFactory=DocumentBuilderFactory.newInstance();
		DocumentBuilder builder=builderFactory.newDocumentBuilder();
		Document document=builder.parse("day01/book.xml");
		
		Element book=(Element) document.getElementsByTagName("书").item(0);
		String name="评价";
		String value="好书啊";
		book.setAttribute(name, value);
		String s=book.getAttribute("评价");
		System.out.println(s);
		
		TransformerFactory factory=TransformerFactory.newInstance();
		Transformer transformer=factory.newTransformer();
		transformer.transform(new DOMSource(document), new StreamResult(new FileOutputStream("day01/book.xml")));
	}
	//删除标签
	@Test
	public void delete() throws Exception
	{
		DocumentBuilderFactory builderFactory=DocumentBuilderFactory.newInstance();
		DocumentBuilder builder=builderFactory.newDocumentBuilder();
		Document document=builder.parse("day01/book.xml");
		
		Element book=(Element) document.getElementsByTagName("作者").item(0);
		book.getParentNode().removeChild(book);
		
		TransformerFactory factory=TransformerFactory.newInstance();
		Transformer transformer=factory.newTransformer();
		transformer.transform(new DOMSource(document), new StreamResult(new FileOutputStream("day01/book.xml")));
		
	}

}



得到代表文档的document对象

36ppt

更新

40ppt

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值