JAVA学习笔记 -- 读写XML
XML是一种可扩展标记语言
以下是一个完整的XML文件(也是下文介绍读写XML的样本):
<? xml version="1.0" encoding="UTF-8"? >
<poem author="William Carlos Williams" title="The Great Figure">
<line>Among the rain</line>
<line>and ligths</line>
<line>I saw the figure 5</line>
<line>in gold</line>
<line>on a red</line>
<line>fire truck</line>
<line>moving</line>
<line>tense</line>
<line>unheeded</line>
<line>to gong clangs</line>
<line>siren howls</line>
<line>and wheels rumbling</line>
<line>through the dark city</line>
</poem>
一、写XML
本文介绍两种方式:使用DOM开发包来写XML文件和用String对象的方式
将Poem类作为数据源,提供须要转换成XML的内容:
class Poem {
	private static String title = "The Great Figure";
	private static String author = "William Carlos Williams";
	private static ArrayList<String> lines = new ArrayList<String>();
	static {
		lines.add("Among the rain");
		lines.add("and ligths");
		lines.add("I saw the figure 5");
		lines.add("in gold");
		lines.add("on a red");
		lines.add("fire truck");
		lines.add("moving");
		lines.add("tense");
		lines.add("unheeded");
		lines.add("to gong clangs");
		lines.add("siren howls");
		lines.add("and wheels rumbling");
		lines.add("through the dark city");
	}
	public static String getTitle() {
		return title;
	}
	public static String getAuthor() {
		return author;
	}
	public static ArrayList<String> getLines() {
		return lines;
	}
}
1、用DOM写XML文件
流程:
(1)创建一个空的Document对象(最顶层的DOM对象,包括了创建XML所须要的其它一切)。
(2)创建元素和属性,把元素和属性加到Document对象中。
(3)把Document对象的内容转换成String对象。
(4)把String对象写到目标文件中去。
import java.util.ArrayList;
import java.io.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.*; public class XmlTest {
public static void main(String[] args) {
Document doc = createXMLContent1(); // 创建空文档
createElements(doc); // 创建XML
String xmlContent = createXMLString(doc);// 创建字符串以表示XML
writeXMLToFile1(xmlContent);
} /*********** 用DOM写XML文件 ***********/
private static Document createXMLContent1() {
Document doc = null;
try {
// 使应用程序可以从XML文档获取生成 DOM 对象树的解析器
DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
doc = docBuilder.newDocument();
// 作为 XML 声明 的一部分指定此文档是否是单独的的属性。 未指定时,此属性为 false。
doc.setXmlStandalone(true);
} catch (ParserConfigurationException pce) {
System.out.println("Couldn't create a DocumentBuilder");
System.exit(1);
}
return doc;
}
private static void createElements(Document doc) {
// 创建根元素
Element poem = doc.createElement("poem");
poem.setAttribute("title", Poem.getTitle());
poem.setAttribute("author", Poem.getAuthor());
// 把根元素加到文档里去
doc.appendChild(poem);
// 创建子元素
for (String lineIn : Poem.getLines()) {
Element line = doc.createElement("line");
Text lineText = doc.createTextNode(lineIn);
line.appendChild(lineText);
// 把每一个元素加到根元素里去
poem.appendChild(line);
}
}
private static String createXMLString(Document doc) {
// 将DOM转换成字符串
Transformer transformer = null;
StringWriter stringWriter = new StringWriter();
try {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
transformer = transformerFactory.newTransformer();
// 是否应输出 XML 声明
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
// 是否以XML格式自己主动换行
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
// 创建字符串以包括XML
stringWriter = new StringWriter();
StreamResult result = new StreamResult(stringWriter);// 充当转换结果的持有者
DOMSource source = new DOMSource(doc);
transformer.transform(source, result);
} catch (TransformerConfigurationException e) {
System.out.println("Couldn't create a Transformer");
System.exit(1);
} catch (TransformerException e) {
System.out.println("Couldn't transforme DOM to a String");
System.exit(1);
}
return stringWriter.toString();
}
private static void writeXMLToFile1(String xmlContent) {
String fileName = "E:\\test\\domoutput.xml";
try {
File domOutput = new File(fileName);
FileOutputStream domOutputStream = new FileOutputStream(domOutput);
domOutputStream.write(xmlContent.getBytes());
domOutputStream.close();
System.out.println(fileName + " was successfully written");
} catch (FileNotFoundException e) {
System.out.println("Couldn't find a file called" + fileName);
System.exit(1);
} catch (IOException e) {
System.out.println("Couldn't write a file called" + fileName);
System.exit(1);
}
}
2、用String写XML文件
这样的方法就比較简单。就是直接用字符串把整个XML文件描写叙述出来,然后保存文件。
二、读取XML文件
两种方式:用DOM读取XML文件和用SAX方式。一般DOM处理内容比較小的XML文件。而SAX能够处理随意大小的XML文件。
1、用DOM读取XML文件
public class XmlTest {
	public static void main(String[] args) {
		String fileName = "E:\\test\\domoutput.xml";
		writeFileContentsToConsole(fileName);
	}
	/*********** 用DOM读取XML文件 ***********/
	private static void writeFileContentsToConsole(String fileName) {
		Document doc = createDocument(fileName);
		Element root = doc.getDocumentElement();// 获取根元素
		StringBuilder sb = new StringBuilder();
		sb.append("The root element is named:\"" + root.getNodeName() + "\"");
		sb.append("and has the following attributes: ");
		NamedNodeMap attributes = root.getAttributes();
		for (int i = 0; i < attributes.getLength(); i++) {
			Node thisAttribute = attributes.item(i);
			sb.append(thisAttribute.getNodeName());
			sb.append("(\"" + thisAttribute.getNodeValue() + "\")");
			if (i < attributes.getLength() - 1) {
				sb.append(",");
			}
		}
		System.out.println(sb);// 根元素的描写叙述信息
		NodeList nodes = doc.getElementsByTagName("line");
		for (int i = 0; i < nodes.getLength(); i++) {
			Element element = (Element) nodes.item(i);
			System.out.println("Found an element named \""
					+ element.getTagName() + "\""
					+ "With the following content: \""
					+ element.getTextContent() + "\"");
		}
	}
	private static Document createDocument(String fileName) {// 从文件创建DOM的Document对象
		Document doc = null;
		try {
			File xmlFile = new File(fileName);
			DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
			doc = docBuilder.parse(xmlFile);// 解析xml文件载入为dom文档
			doc.setXmlStandalone(true);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
		return doc;
	}
}/*
 * Output:
 * The root element is named:"poem"and has the following attributes: author("William Carlos Williams"),title("The Great Figure")
 * Found an element named "line"With the following content: "Among the rain"
 * Found an element named "line"With the following content: "and ligths"
 * ... ...
 */// :~ 
2、用SAX读取XML文件
SAX是使用ContentHandler接口来公开解析事件,并且SAX包提供了一个默认实现类DefaultHandler,它的默认行为就是什么都不做。以下就通过XMLToConsoleHandler类来覆盖当中的一些方法,来捕获XML文件的内容。
import org.w3c.dom.CharacterData;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class XmlTest {
public static void main(String[] args) {
String fileName = "E:\\test\\domoutput.xml";
getFileContents(fileName);
}
private static void getFileContents(String fileName) {
try {
XMLToConsoleHandler handler = new XMLToConsoleHandler();
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
saxParser.parse(fileName, handler);
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
}
}
/*********** 用SAX读取XML文件 ***********/
class XMLToConsoleHandler extends DefaultHandler {
public void characters(char[] content, int start, int length)
throws SAXException { // 处理元素的真正内容
System.out.println("Found content: " + new String(content, start, length));
}
public void endElement(String arg0, String localName, String qName)throws SAXException {
System.out.println("Found the end of an element named \"" + qName + "\"");
}
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
StringBuilder sb = new StringBuilder();
sb.append("Found the start of an element named \"" + qName + "\"");
if (attributes != null && attributes.getLength() > 0) {
sb.append(" with attributes named ");
for (int i = 0; i < attributes.getLength(); i++) {
String attributeName = attributes.getLocalName(i);
String attributeValue = attributes.getValue(i);
sb.append("\"" + attributeName + "\"");
sb.append(" (value = ");
sb.append("\"" + attributeValue + "\"");
sb.append(")");
if (i < attributes.getLength() - 1) {
sb.append(",");
}
}
}
System.out.println(sb.toString());
}
}
JAVA学习笔记 -- 读写XML的更多相关文章
- Java学习笔记——File类之文件管理和读写操作、下载图片
		
Java学习笔记——File类之文件管理和读写操作.下载图片 File类的总结: 1.文件和文件夹的创建 2.文件的读取 3.文件的写入 4.文件的复制(字符流.字节流.处理流) 5.以图片地址下载图 ...
 - 《Java学习笔记(第8版)》学习指导
		
<Java学习笔记(第8版)>学习指导 目录 图书简况 学习指导 第一章 Java平台概论 第二章 从JDK到IDE 第三章 基础语法 第四章 认识对象 第五章 对象封装 第六章 继承与多 ...
 - Java学习笔记4
		
Java学习笔记4 1. JDK.JRE和JVM分别是什么,区别是什么? 答: ①.JDK 是整个Java的核心,包括了Java运行环境.Java工具和Java基础类库. ②.JRE(Java Run ...
 - java学习笔记16--I/O流和文件
		
本文地址:http://www.cnblogs.com/archimedes/p/java-study-note16.html,转载请注明源地址. IO(Input Output)流 IO流用来处理 ...
 - 20145230《java学习笔记》第九周学习总结
		
20145230 <Java程序设计>第9周学习总结 教材学习内容 JDBC JDBC简介 JDBC是用于执行SQL的解决方案,开发人员使用JDBC的标准接口,数据库厂商则对接口进行操作, ...
 - Java学习笔记之---Servlet
		
Java学习笔记之---Servlet (一)如何实现Servlet 1.实现javax.servlet.Servlet接口: 2.继承javax.servlet.GenericServlet类: 3 ...
 - 0037 Java学习笔记-多线程-同步代码块、同步方法、同步锁
		
什么是同步 在上一篇0036 Java学习笔记-多线程-创建线程的三种方式示例代码中,实现Runnable创建多条线程,输出中的结果中会有错误,比如一张票卖了两次,有的票没卖的情况,因为线程对象被多条 ...
 - 0035 Java学习笔记-注解
		
什么是注解 注解可以看作类的第6大要素(成员变量.构造器.方法.代码块.内部类) 注解有点像修饰符,可以修饰一些程序要素:类.接口.变量.方法.局部变量等等 注解要和对应的配套工具(APT:Annot ...
 - Java学习笔记(04)
		
Java学习笔记(04) 如有不对或不足的地方,请给出建议,谢谢! 一.对象 面向对象的核心:找合适的对象做合适的事情 面向对象的编程思想:尽可能的用计算机语言来描述现实生活中的事物 面向对象:侧重于 ...
 
随机推荐
- BZOJ2120/洛谷P1903 [国家集训队] 数颜色 [带修改莫队]
			
BZOJ传送门:洛谷传送门 数颜色 题目描述 墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会向你发布如下指令: 1. Q L R代表询问你从第L支画笔到第R ...
 - 推荐Maven的两个仓库
			
概述 推荐两个maven的仓库,可用于查找依赖,下载jar包. 正文 mvnrepository 这个仓库用来检索依赖.下载jar包:网址:http://mvnrepository.com/ 仓库的主 ...
 - 线段树+扫描线【bzoj1645】[USACO07OPEN]城市的地平线City Horizon
			
Description 约翰带着奶牛去都市观光.在落日的余晖里,他们看到了一幢接一幢的摩天高楼的轮廓在地平线 上形成美丽的图案.以地平线为 X 轴,每幢高楼的轮廓是一个位于地平线上的矩形,彼此间可能有 ...
 - Java编程思想学习(一)----对象导论中多态的理解
			
1.1抽象过程 1)万物皆对象. 2)程序是对象的集合,他们通过发送消息来告知彼此所要求做的. 3)每个对象都有自己的由其他对象所构成的存储. 4)每个对象都拥有其类型. 5)某一特定类型的所有对象都 ...
 - usaco-2.2.2Subset Sums 集合
			
01背包,对每个数至多取一次,为了避免重复,应倒序dp usaco-2.2.2Subset Sums 集合 时间限制: 1 Sec 内存限制: 128 MB 题目描述 对于从1到N的连续整集合合,能 ...
 - C# 高德地图调用帮助类 GaodeHelper
			
/// <summary> /// 高德地图调用帮助类 /// 更多详情请参考 高德api /// </summary> public class GaodeHelper { ...
 - NHibernate官方文档中文版——批量插入(Batch inserts)
			
A naive approach t7o inserting 100 000 rows in the database using NHibernate might look like this: 一 ...
 - Dell最近的几款显示器看上去还不错的样子
			
发现最近戴尔最近发布了两款的4k显示器P2415Q和P2715Q,价格还比较平易近人,淘宝价24寸3700,27寸4700,让人有点心动了.基本参数如下: 3840*2160分辨率 亮度350cd/m ...
 - Do waiting or suspended tasks tie up a worker thread?
			
https://blogs.msdn.microsoft.com/askjay/2012/07/29/do-waiting-or-suspended-tasks-tie-up-a-worker-t ...
 - Email the output of a concurrent program as Attachment
			
This article illustrates the steps to be followed to Email a concurrent program's output. Write a pr ...