jaxp解析xml

有一个xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?><书架>
	<书>
		<书名 name="xxxx">java web</书名>
		<作者>张大</作者>
		<价格>99</价格>
	</书>
	<书>
		<书名>spring</书名>
		<作者>呜呜</作者>
		<价格>55</价格>
	</书>
</书架>

读取指定节点的内容

	public void read1() throws Exception{
		//创建工厂
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		//得到Dom解析器
		DocumentBuilder builder = factory.newDocumentBuilder();
		//得到代表文档的document
		Document document = builder.parse("src/book.xml");
		
		NodeList list = document.getElementsByTagName("书名");
		Node node = list.item(1);
		String content = node.getTextContent();
		System.out.println(content);
		
	}

遍历所有标签

	public void read2() throws Exception{
		
		//创建工厂
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		//得到Dom解析器
		DocumentBuilder builder = factory.newDocumentBuilder();
		//得到代表文档的document
		Document document = builder.parse("src/book.xml");
		
		Node root = document.getElementsByTagName("书架").item(0);
		
		traverse(root);
		
	}

	private void traverse(Node node) {
		
		if(node instanceof Element){
			System.out.println(node.getNodeName());
		}
		NodeList list = node.getChildNodes();
		for(int i=0;i<list.getLength();i++){
			Node child = list.item(i);
			traverse(child);
		}
	}

得到标签属性

	public void read3() throws Exception{
		//创建工厂
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		//得到Dom解析器
		DocumentBuilder builder = factory.newDocumentBuilder();
		//得到代表文档的document
		Document document = builder.parse("src/book.xml");
		
		Element bookname =  (Element) document.getElementsByTagName("书名").item(0);
		String value = bookname.getAttribute("name");
		System.out.println(value);
	}

在指定位子添加节点,不指定加在最后一个位子

	public void add() throws Exception{
		//创建工厂
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		//得到Dom解析器
		DocumentBuilder builder = factory.newDocumentBuilder();
		//得到代表文档的document
		Document document = builder.parse("src/book.xml");
		
		//价格上添加评价
		Element evaluate = document.createElement("评价");
		evaluate.setTextContent("well");
		//得到参考节点
		Element reNode = (Element) document.getElementsByTagName("价格").item(0);
		//得到父节点
		Element book =  (Element) document.getElementsByTagName("书").item(0);
		//父节点中插入
		book.insertBefore(evaluate, reNode);
		
		//更新后的内存写入xml文档
		TransformerFactory tf_factory =  TransformerFactory.newInstance();
		Transformer t = tf_factory.newTransformer();
		t.transform(new DOMSource(document), new StreamResult(new FileOutputStream("src/book.xml")));
	}

删除节点

<span style="white-space:pre">	</span>public void delete() throws Exception{
		//创建工厂
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		//得到Dom解析器
		DocumentBuilder builder = factory.newDocumentBuilder();
		//得到代表文档的document
		Document document = builder.parse("src/book.xml");
		
		//得到要删除的节点
		Element price =  (Element) document.getElementsByTagName("价格").item(0);
		//得到父节点
		Element book =  (Element) document.getElementsByTagName("书").item(0);
		
		//删
		book.removeChild(price);
		//更新后的内存写入xml文档
		TransformerFactory tf_factory =  TransformerFactory.newInstance();
		Transformer t = tf_factory.newTransformer();
		t.transform(new DOMSource(document), new StreamResult(new FileOutputStream("src/book.xml")));
	}

第二种删除方法

	public void delete2() throws Exception{
		//创建工厂
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		//得到Dom解析器
		DocumentBuilder builder = factory.newDocumentBuilder();
		//得到代表文档的document
		Document document = builder.parse("src/book.xml");
		
		//得到要删除的节点
		Element e =  (Element) document.getElementsByTagName("价格").item(0);
		e.getParentNode().removeChild(e);
	
		//更新后的内存写入xml文档
		TransformerFactory tf_factory =  TransformerFactory.newInstance();
		Transformer t = tf_factory.newTransformer();
		t.transform(new DOMSource(document), new StreamResult(new FileOutputStream("src/book.xml")));
	}

更新节点

	@Test	
	public void update() throws Exception{
		//创建工厂
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		//得到Dom解析器
		DocumentBuilder builder = factory.newDocumentBuilder();
		//得到代表文档的document
		Document document = builder.parse("src/book.xml");
		
		//得到要更新的节点
		Element e =  (Element) document.getElementsByTagName("价格").item(0);
		e.setTextContent("102");
	
		//更新后的内存写入xml文档
		TransformerFactory tf_factory =  TransformerFactory.newInstance();
		Transformer t = tf_factory.newTransformer();
		t.transform(new DOMSource(document), new StreamResult(new FileOutputStream("src/book.xml")));
	}



你可能感兴趣的:(xml,dom,jaxp)