利用XML的dom进行数据的增删改查

前端之家收集整理的这篇文章主要介绍了利用XML的dom进行数据的增删改查前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
importjava.io.FileOutputStream;

importjavax.swing.event.DocumentListener;
importjavax.xml.parsers.DocumentBuilder;
importjavax.xml.parsers.DocumentBuilderFactory;
importjavax.xml.parsers.ParserConfigurationException;
importjavax.xml.transform.Transformer;
importjavax.xml.transform.TransformerFactory;
importjavax.xml.transform.dom.DOMSource;
importjavax.xml.transform.stream.StreamResult;

importorg.junit.Test;
importorg.w3c.dom.Document;
importorg.w3c.dom.Element;
importorg.w3c.dom.Node;
importorg.w3c.dom.NodeList;

//使用DOM方式对XML文档进行CRUD
publicclassDemo2{
	
	//查询数据
	@Test
	publicvoidread()throwsException{
		//1:创建工厂
		DocumentBuilderFactoryfactory=DocumentBuilderFactory.newInstance();
		
		//2:得到DOM解析器
		DocumentBuilderbuilder=factory.newDocumentBuilder();
		
		//3:解析XML文档,得到代表文档的document
		Documentdocument=builder.parse("src/book.xml");
		
		NodeListlist=document.getElementsByTagName("书名");
		Nodenode=list.item(1);
		Stringcontent=node.getTextContent();
		System.out.println(content);
	}
	
	//添加数据
	@Test
	publicvoidadd()throwsException{
		DocumentBuilderFactoryfactory=DocumentBuilderFactory.newInstance();
		DocumentBuilderbuilder=factory.newDocumentBuilder();
		Documentdocument=builder.parse("src/book.xml");
		
		//创建节点
		Elementprice=document.createElement("售价");
		price.setTextContent("3.15元");

		//把创建的节点挂在第一本书上
		Elementbook=(Element)document.getElementsByTagName("书").item(0);
		book.appendChild(price);
		
		//把更新后内存写会到XML文档
		TransformerFactorytffactory=TransformerFactory.newInstance();
		Transformertf=tffactory.newTransformer();
		tf.transform(newDOMSource(document),newStreamResult(newFileOutputStream("src/book.xml")));
	}
	
	//向指定位置添加节点
	@Test
	publicvoidadd1()throwsException{
		DocumentBuilderFactoryfactory=DocumentBuilderFactory.newInstance();
		DocumentBuilderbuilder=factory.newDocumentBuilder();
		Documentdocument=builder.parse("src/book.xml");
		
		//创建节点
		Elementprice=document.createElement("售价");
		price.setTextContent("5.25元");
		
		//得到参考节点
		ElementrefNode=(Element)document.getElementsByTagName("售价").item(0);
		
		//得到要挂载的节点
		Elementbook=(Element)document.getElementsByTagName("书").item(0);
		
		//往book节点的指定位置插入
		book.insertBefore(price,refNode);
		
		//把更新后内存写会到XML文档
		TransformerFactorytffactory=TransformerFactory.newInstance();
		Transformertf=tffactory.newTransformer();
		tf.transform(newDOMSource(document),newStreamResult(newFileOutputStream("src/book.xml")));
	}
	
	//向XML文档节点上添加属性
		@Test
		publicvoidaddattr()throwsException{
			DocumentBuilderFactoryfactory=DocumentBuilderFactory.newInstance();
			DocumentBuilderbuilder=factory.newDocumentBuilder();
			Documentdocument=builder.parse("src/book.xml");
			
			//得到参考节点
			ElementbookName=(Element)document.getElementsByTagName("书名");
			bookName.setAttribute("name","XXXXX");
			
			//把更新后内存写会到XML文档
			TransformerFactorytffactory=TransformerFactory.newInstance();
			Transformertf=tffactory.newTransformer();
			tf.transform(newDOMSource(document),newStreamResult(newFileOutputStream("src/book.xml")));
		}
	
		//删除单个节点
		@Test
		publicvoiddelete()throwsException{
			DocumentBuilderFactoryfactory=DocumentBuilderFactory.newInstance();
			DocumentBuilderbuilder=factory.newDocumentBuilder();
			Documentdocument=builder.parse("src/book.xml");
			
			//先得到要删除的节点
			ElementdelE=(Element)document.getElementsByTagName("售价").item(0);
			
			//得到要删除节点的爸爸
			Elementbook=(Element)document.getElementsByTagName("书").item(0);
			
			//爸爸删除
			book.removeChild(delE);
			
		}
		//删除全部节点
		@Test
		publicvoiddeleteAll()throwsException{
			DocumentBuilderFactoryfactory=DocumentBuilderFactory.newInstance();
			DocumentBuilderbuilder=factory.newDocumentBuilder();
			Documentdocument=builder.parse("src/book.xml");
			
			//先得到要删除的节点
			Elementbook=(Element)document.getElementsByTagName("售价").item(0);
			book.getParentNode().getParentNode().getParentNode().removeChild(book.getParentNode().getParentNode());
		
			//把更新后内存写会到XML文档
			TransformerFactorytffactory=TransformerFactory.newInstance();
			Transformertf=tffactory.newTransformer();
			tf.transform(newDOMSource(document),newStreamResult(newFileOutputStream("src/book.xml")));

		}
		
		//更新售价
		@Test
		publicvoidupdate()throwsException{
			DocumentBuilderFactoryfactory=DocumentBuilderFactory.newInstance();
			DocumentBuilderbuilder=factory.newDocumentBuilder();
			Documentdocument=builder.parse("src/book.xml");
			
			Elementbook=(Element)document.getElementsByTagName("售价").item(0);	
			book.setTextContent("105");
			
			//把更新后内存写会到XML文档
			TransformerFactorytffactory=TransformerFactory.newInstance();
			Transformertf=tffactory.newTransformer();
			tf.transform(newDOMSource(document),newStreamResult(newFileOutputStream("src/book.xml")));

		}
}

书的XML

<?xmlversion="1.0"encoding="UTF-8"standalone="no"?><书架>
	<书>
		<书名>Java培训</书名>
		<作者>张扬</作者>
		<售价>5.25元</售价>
	</书>

	<书>
		<书名>Java培训2</书名>
		<作者>张扬</作者>
		<售价>21.5</售价>
	</书>
</书架>
原文链接:https://www.f2er.com/xml/293569.html

猜你在找的XML相关文章