JAVA操作XML文件 - DOM操作

作者:流沙 分类: Android 发布于:2016-6-11 22:13 ė523次浏览 60条评论

主要是CRUD操作 U操作,没有更新!




package com.gyarmy.dom;

import java.io.File;
import java.io.IOException;

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

import junit.framework.Assert;

import org.junit.Test;
import org.w3c.dom.*;
import org.xml.sax.SAXException;

public class TestDom {

	//获取内容
	@Test
	public void testReadXML() throws ParserConfigurationException, SAXException, IOException{
		Document document = getDocument();
		
		NodeList nl = document.getElementsByTagName("书名");
		
		int length = nl.getLength();
		
		//System.out.println(length);
		Node firstNode = nl.item(0);
		String result = firstNode.getTextContent();
		//System.out.println(result);
		Assert.assertEquals("明朝那些事儿", result);
		
	}
	
	//获取属性
	@Test 
	public void testReadAttribute() throws ParserConfigurationException, SAXException, IOException{
		//获取document;
		Document document = getDocument();
		NodeList nl = document.getElementsByTagName("书");
		Element firstNode = (Element)nl.item(0);
		String result = firstNode.getAttribute("出版社");
		Assert.assertEquals("深圳出版社", result);
		
	}
	
	//添加节点
	@Test
	public void testAddPrice() throws ParserConfigurationException, SAXException, IOException, TransformerException
	{
		Document document = getDocument();
		//创建节点
		Element newElement = document.createElement("price");
		newElement.setTextContent("打折9.8");
		
		//得到节点
		NodeList nl = document.getElementsByTagName("书");
		Node firstNode = nl.item(0);
		//添加节点
		firstNode.appendChild(newElement);
		writeBackToXML(document);
	}

	
	
	//删除节点
	@Test
	public void testRemovePrice() throws ParserConfigurationException, SAXException, IOException, TransformerException
	{
		Document document = getDocument();
		//得到节点
		NodeList nlk = document.getElementsByTagName("price");
		Element delElement = (Element)nlk.item(0);
		
		//得到节点
		NodeList nl = document.getElementsByTagName("书");
		Node firstNode = nl.item(0);
		//删除节点
		firstNode.removeChild(delElement);
		
		writeBackToXML(document);
	}
	
	//更新节点
	@Test
	public void updatePrice() throws ParserConfigurationException, SAXException, IOException
	{
	
	}
	
	//打印及诶单元素
	@Test
	
	public void printAllElements()throws ParserConfigurationException, SAXException, IOException
	{
		
		Document document = getDocument();
		NodeList nl = document.getElementsByTagName("书");
		for(int i=0;i<nl.getLength();i++)
		{
			Node node = nl.item(i).getFirstChild();
			while(true)
			{
				Node nextNode = node.getNextSibling();
				if(nextNode == null)
				{
					break;
				}
				String nodeName = nextNode.getNodeName();
				if(nodeName!="#text")
				{
					String nodeValue = nextNode.getTextContent();
					System.out.println(nodeName+" : "+nodeValue);
				}
				node = nextNode;
			}
			System.out.println();
		}
		
		System.out.println("www.gyarmy.com");
	}
	
	@Test
	public void printElementName() throws ParserConfigurationException, SAXException, IOException
	{
		Document document = getDocument();
		printAllElementsName(document);
	}
	public void printAllElementsName(Node node)
	{
		if(Node.ELEMENT_NODE == node.getNodeType())
		{
			System.out.println(node.getNodeName());
		}
		
		NodeList nodes = node.getChildNodes();
		for(int i=0;i<nodes.getLength();i++)
		{
			//System.out.println(nodes.item(i).getNodeName());
			
			printAllElementsName(nodes.item(i));
		}
		
		
	}

	private Document getDocument() throws ParserConfigurationException,
			SAXException, IOException {
		//获得XML对象
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		Document document = builder.parse("src/demo1.xml");
		return document;
	}
	
	private void writeBackToXML(Document document)
			throws TransformerFactoryConfigurationError,
			TransformerConfigurationException, TransformerException {
		//写入文件
		//TransformerFactory.newTransformer 
		TransformerFactory factory = TransformerFactory.newInstance();
		Transformer tf = factory.newTransformer();
		
		tf.transform(new DOMSource(document),new StreamResult(new File("src/demo1.xml")));
	}
	
}


本文出自 流沙,转载时请注明出处及相应链接。

发表评论

电子邮件地址不会被公开。必填项已用*标注


Ɣ回顶部