package test;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class Test {
public static void main(String[] args) {
Test t = new Test();
File file = new File("C:\\Users\\Administrator\\Desktop\\test.xml");
Book b1 = new Book(1, "一千零一夜", "安徒生", 51);
Book b2 = new Book(2, "一千零二夜", "安徒生", 52);
Book b3 = new Book(3, "一千零三夜", "安徒生", 53);
Book b4 = new Book(4, "一千零四夜", "安徒生", 54);
Book b5 = new Book(5, "一千零5夜", "安徒生", 55);
List<Book> list = new ArrayList<Book>();
list.add(b1);
list.add(b2);
list.add(b3);
list.add(b4);
list.add(b5);
Document document = t.createXMLByDOM(list);
t.transformerXML(document, file);
t.disassembleXML("C:\\Users\\Administrator\\Desktop\\test.xml");
}
public Document createXMLByDOM(List<Book> list) {
// 创建DocumentBuilderFactory
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
// 创建DocumentBuilder
DocumentBuilder builder = factory.newDocumentBuilder();
// 创建Document
Document document = builder.newDocument();
// 设置XML声明中standalone为yes,即没有dtd和schema作为该XML的说明文档,且不显示该属性
document.setXmlStandalone(true);
// 创建根节点
Element bookstore = document.createElement("bookstore");
// 创建子节点,并设置属性
for (int i = 0; i < list.size(); i++) {
Element book = document.createElement("book");
Element id = document.createElement("id");
id.setTextContent(String.valueOf(list.get(i).getId()));
book.appendChild(id);
Element name = document.createElement("name");
name.setTextContent(list.get(i).getName());
book.appendChild(name);
Element author = document.createElement("author");
author.setTextContent(list.get(i).getAuthor());
book.appendChild(author);
Element price = document.createElement("price");
price.setTextContent(String.valueOf(list.get(i).getPrice()));
book.appendChild(price);
bookstore.appendChild(book);
}
// 将根节点添加到Document下
document.appendChild(bookstore);
return document;
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
return null;
}
public void transformerXML(Document document, File dest) {
// 创建TransformerFactory对象
TransformerFactory tff = TransformerFactory.newInstance();
// 创建Transformer对象
Transformer tf = null;
try {
tf = tff.newTransformer();
} catch (TransformerConfigurationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// 设置输出数据时换行
tf.setOutputProperty(OutputKeys.INDENT, "yes");
try {
tf.transform(new DOMSource(document), new StreamResult(dest));
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void disassembleXML(String filePath) {
// 创建DocumentBuilderFactory
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
Document docu = db.parse(filePath);
// 获取所有的书籍节点
NodeList booklist = docu.getElementsByTagName("book");
for (int i = 0; i < booklist.getLength(); i++) {
Node book_item = booklist.item(i);
System.out.println("第" + (i + 1) + "本书");
NamedNodeMap node_att = book_item.getAttributes();// 读取属性,并存在一个<span
// style="font-family:
// Arial,
// Helvetica,
// sans-serif;">NamedNodeMap中</span>
for (int j = 0; j < node_att.getLength(); j++) {
Node node = node_att.item(j);
System.out.print(node.getNodeName() + ":" + node.getNodeValue() + " ");
System.out.println();
}
NodeList book_child = book_item.getChildNodes();
for (int k = 0; k < book_child.getLength(); k++) {
Node book_child_ele = book_child.item(k);
if (book_child_ele.getNodeType() == Node.ELEMENT_NODE) {// 如果没有会打印出很多空格,因为text也是一种节点类型,
// System.out.println(book_child_ele.getNodeName()+":"+book_child_ele.getFirstChild().getNodeValue());
这个就是采集到这个<name></name>中所有的所有的text
System.out.println(book_child_ele.getNodeName() + ":" + book_child_ele.getTextContent());
}
}
System.out.println("以上就是第" + (i + 1) + "本书");
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class Test {
public static void main(String[] args) {
Test t = new Test();
File file = new File("C:\\Users\\Administrator\\Desktop\\test.xml");
Book b1 = new Book(1, "一千零一夜", "安徒生", 51);
Book b2 = new Book(2, "一千零二夜", "安徒生", 52);
Book b3 = new Book(3, "一千零三夜", "安徒生", 53);
Book b4 = new Book(4, "一千零四夜", "安徒生", 54);
Book b5 = new Book(5, "一千零5夜", "安徒生", 55);
List<Book> list = new ArrayList<Book>();
list.add(b1);
list.add(b2);
list.add(b3);
list.add(b4);
list.add(b5);
Document document = t.createXMLByDOM(list);
t.transformerXML(document, file);
t.disassembleXML("C:\\Users\\Administrator\\Desktop\\test.xml");
}
public Document createXMLByDOM(List<Book> list) {
// 创建DocumentBuilderFactory
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
// 创建DocumentBuilder
DocumentBuilder builder = factory.newDocumentBuilder();
// 创建Document
Document document = builder.newDocument();
// 设置XML声明中standalone为yes,即没有dtd和schema作为该XML的说明文档,且不显示该属性
document.setXmlStandalone(true);
// 创建根节点
Element bookstore = document.createElement("bookstore");
// 创建子节点,并设置属性
for (int i = 0; i < list.size(); i++) {
Element book = document.createElement("book");
Element id = document.createElement("id");
id.setTextContent(String.valueOf(list.get(i).getId()));
book.appendChild(id);
Element name = document.createElement("name");
name.setTextContent(list.get(i).getName());
book.appendChild(name);
Element author = document.createElement("author");
author.setTextContent(list.get(i).getAuthor());
book.appendChild(author);
Element price = document.createElement("price");
price.setTextContent(String.valueOf(list.get(i).getPrice()));
book.appendChild(price);
bookstore.appendChild(book);
}
// 将根节点添加到Document下
document.appendChild(bookstore);
return document;
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
return null;
}
public void transformerXML(Document document, File dest) {
// 创建TransformerFactory对象
TransformerFactory tff = TransformerFactory.newInstance();
// 创建Transformer对象
Transformer tf = null;
try {
tf = tff.newTransformer();
} catch (TransformerConfigurationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// 设置输出数据时换行
tf.setOutputProperty(OutputKeys.INDENT, "yes");
try {
tf.transform(new DOMSource(document), new StreamResult(dest));
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void disassembleXML(String filePath) {
// 创建DocumentBuilderFactory
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
Document docu = db.parse(filePath);
// 获取所有的书籍节点
NodeList booklist = docu.getElementsByTagName("book");
for (int i = 0; i < booklist.getLength(); i++) {
Node book_item = booklist.item(i);
System.out.println("第" + (i + 1) + "本书");
NamedNodeMap node_att = book_item.getAttributes();// 读取属性,并存在一个<span
// style="font-family:
// Arial,
// Helvetica,
// sans-serif;">NamedNodeMap中</span>
for (int j = 0; j < node_att.getLength(); j++) {
Node node = node_att.item(j);
System.out.print(node.getNodeName() + ":" + node.getNodeValue() + " ");
System.out.println();
}
NodeList book_child = book_item.getChildNodes();
for (int k = 0; k < book_child.getLength(); k++) {
Node book_child_ele = book_child.item(k);
if (book_child_ele.getNodeType() == Node.ELEMENT_NODE) {// 如果没有会打印出很多空格,因为text也是一种节点类型,
// System.out.println(book_child_ele.getNodeName()+":"+book_child_ele.getFirstChild().getNodeValue());
这个就是采集到这个<name></name>中所有的所有的text
System.out.println(book_child_ele.getNodeName() + ":" + book_child_ele.getTextContent());
}
}
System.out.println("以上就是第" + (i + 1) + "本书");
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}