XML学习经验实例总结2

前端之家收集整理的这篇文章主要介绍了XML学习经验实例总结2前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
DTD约束
Book.dtd:
<!ELEMENT 书架 (书+)>
<!ELEMENT 书 (书名,价格,介绍)>
<!ELEMENT 书名 (#PCDATA)>
<!ELEMENT 价格 (#PCDATA)>
<!ELEMENT 介绍 (#PCDATA)>						为书名设置属性 name(必须)
<!ATTLIST 书名  name CDATA #required>

Book.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE 书架 
SYSTEM "book.dtd">
<书架>
	<书>
		<书名 name="wy">计算机科学与技术</书名>
		<价格>36.0</价格>
		<介绍>这本书很好</介绍>
	</书>
	<书>
		<书名 name="ztt">科学管理课</书名>
		<价格>30.0</价格>
		<介绍>这本书挺好</介绍>
	</书>
	<书>
		<书名 name="wu">李清照集注</书名>
		<价格>60.0</价格>
		<介绍>这本书非常好</介绍>
	</书>
</书架>
Jaxp对xml进行DOM解析---读取
(1)获取XML中的书名

public void read() throws Exception {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();

		Document document = builder.parse("src/book.xml");

		NodeList list = document.getElementsByTagName("书名");
		Node node = list.item(1);
		String content = node.getTextContent();
		System.out.println(content);

	}


(2)获取所有的节点
public void read2() throws Exception {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();

		Document document = builder.parse("src/book.xml");

		Node root = document.getElementsByTagName("书架").item(0);
		list(root);

	}

	private void list(Node root) {
		if(root instanceof Element)
			System.out.println(root.getNodeName());
		NodeList list=root.getChildNodes();
		for(int i=0;i<list.getLength();i++)
		{
			Node child=list.item(i);
			list(child);
		}
	
	}
(3)获取文档中的属性
		public void read3() throws Exception {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();

		Document document = builder.parse("src/book.xml");

		Element bookname =(Element) document.getElementsByTagName("书名").item(0);
		String value=bookname.getAttribute("name");
		System.out.println(value);
	}
XML解析---增加
(1)默认在末尾添加节点
	@Test
	public void add() throws Exception
	{
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();

		Document document = builder.parse("src/book.xml");
		//创建节点
		Element ele=document.createElement("价格");
		ele.setTextContent("10元");
		//创建节点挂在第一个节点上
		Element book=(Element)document.getElementsByTagName("书").item(0);
		book.appendChild(ele);
		//把更新后内存写回到xml文档
		TransformerFactory tffactory=TransformerFactory.newInstance();
		Transformer tf=tffactory.newTransformer();
		tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream("src/book.xml")));
		
	}

(2) 在文档指定位置添加节点
	@Test
	public void add2() throws Exception {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();

		Document document = builder.parse("src/book.xml");
		// 创建节点
		Element ele = document.createElement("价格");
		ele.setTextContent("10元");

		// 得到参考节点
		Element refNode = (Element) document.getElementsByTagName("价格").item(0);

		// 得到要挂的节点
		Element book = (Element) document.getElementsByTagName("书").item(0);

		// 往book节点的指定位置插孩子节点
		book.insertBefore(ele,refNode);
		// 把更新后内存写回到xml文档
		TransformerFactory tffactory = TransformerFactory.newInstance();
		Transformer tf = tffactory.newTransformer();
		tf.transform(new DOMSource(document),new StreamResult(
				new FileOutputStream("src/book.xml")));

	}
(3) 在文档中为标签添加属性
	@Test
	public void addAttr() throws Exception {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();

		Document document = builder.parse("src/book.xml");
		Element bookname = (Element) document.getElementsByTagName("书名")
				.item(0);
		bookname.setAttribute("name","wy");
		TransformerFactory tffactory = TransformerFactory.newInstance();
		Transformer tf = tffactory.newTransformer();
		tf.transform(new DOMSource(document),new StreamResult(
				new FileOutputStream("src/book.xml")));

	}
删除
1.删除1
	@Test
	public void delete1() throws Exception {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();

		Document document = builder.parse("src/book.xml");

		// 得到要删除的节点
		Element ele = (Element) document.getElementsByTagName("价格").item(0);

		// 得到要删除的节点的父节点
		Element book = (Element) document.getElementsByTagName("书").item(0);

		// 父节点删除子节点
		book.removeChild(ele);

		TransformerFactory tffactory = TransformerFactory.newInstance();
		Transformer tf = tffactory.newTransformer();
		tf.transform(new DOMSource(document),new StreamResult(
				new FileOutputStream("src/book.xml")));
	}
(2)删除2
	@Test
	public void delete2() throws Exception {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();

		Document document = builder.parse("src/book.xml");

		// 得到要删除的节点
		Element ele = (Element) document.getElementsByTagName("价格").item(0);
		ele.getParentNode().getParentNode().removeChild(ele.getParentNode());
		
		TransformerFactory tffactory = TransformerFactory.newInstance();
		Transformer tf = tffactory.newTransformer();
		tf.transform(new DOMSource(document),new StreamResult(
				new FileOutputStream("src/book.xml")));
	}
5更新
	@Test
	public void update() throws Exception
	{
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();

		Document document = builder.parse("src/book.xml");

		Element ele=(Element)document.getElementsByTagName("价格").item(0);
		ele.setTextContent("100元");
		
		//把更新后的内存写到xml文档中去
		TransformerFactory tffactory = TransformerFactory.newInstance();
		Transformer tf = tffactory.newTransformer();
		tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream("src/book.xml")));
	}
SAX解析XML
1.步骤:
  //创建工厂
		SAXParserFactory factory=SAXParserFactory.newInstance();
		//得到解析器
		SAXParser sp=factory.newSAXParser();
		//得到读取器
		XMLReader reader=sp.getXMLReader();
		//设置内容处理器
		reader.setContentHandler(new ListHandler());
		//读取xml文档内容
		reader.parse("src/book.xml");
原文链接:https://www.f2er.com/xml/299875.html

猜你在找的XML相关文章