标签:xml java dom
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);
-
String xmlContent = createXMLString(doc);
-
writeXMLToFile1(xmlContent);
-
}
-
-
-
private static Document createXMLContent1() {
-
Document doc = null;
-
try {
-
-
DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
-
DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
-
doc = docBuilder.newDocument();
-
-
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) {
-
-
Transformer transformer = null;
-
StringWriter stringWriter = new StringWriter();
-
try {
-
TransformerFactory transformerFactory = TransformerFactory.newInstance();
-
transformer = transformerFactory.newTransformer();
-
-
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
-
-
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
-
-
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);
-
}
-
-
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) {
-
Document doc = null;
-
try {
-
File xmlFile = new File(fileName);
-
DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
-
DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
-
doc = docBuilder.parse(xmlFile);
-
doc.setXmlStandalone(true);
-
} catch (IOException e) {
-
e.printStackTrace();
-
} catch (SAXException e) {
-
e.printStackTrace();
-
} catch (ParserConfigurationException e) {
-
e.printStackTrace();
-
}
-
return doc;
-
}
-
}
-
-
-
-
-
-
// :~
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();
-
}
-
}
-
}
-
-
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());
-
}
-
}
读写XML,布布扣,bubuko.com
读写XML
标签:xml java dom
原文地址:http://blog.csdn.net/goluck98/article/details/38428087