标签:style end 文件解析 bre efault ase attr ring 硬盘
// 书籍列表
private List<Book> bookList = new LinkedList<Book>();
/**
* 根据xml文件,得到书籍列表
*
* @param file
* @return
*/
public List<Book> getBookList(String file) {
parseXML(file);
return bookList;
}
/**
* 获取DocumentBuilder对象
*@return
*/
private DocumentBuilder getDocumentBuilder() {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return builder;
}
/**
* 读取XML,并将book对象存入书籍列表
*
* @param file
*/
private void parseXML(String file) {
// 1.临时书籍变量
Book book;
try {
// 2.获取DocumentBuilder对象
DocumentBuilder documentBuilder = getDocumentBuilder();
// 3.加载XML文件到当前项目下,获取Document‘文档’对象
Document document = documentBuilder.parse(file);
// 4.获取节点名称是book的集合
NodeList books = document.getElementsByTagName("book");
// 5.遍历book节点集合
for (int i = 0; i < books.getLength(); i++) {
// 重置book变量
book = new Book();
// 5.1声明book节点变量
Node bookNode = books.item(i);
// 5.2获取book节点的属性集合
NamedNodeMap nodeMap = bookNode.getAttributes();
// 5.3遍历属性集合,判断并为book对象的属性赋值
for (int j = 0; j < nodeMap.getLength(); j++) {
// 5.4声明book的属性
Node attrNode = nodeMap.item(j);
// 5.5如果属性Name为id
if (attrNode.getNodeName().equals("id")) {
book.setId(Integer.valueOf(attrNode.getNodeValue()));
}
}
// 5.6获取book节点的子节点
NodeList childList = bookNode.getChildNodes();
// 5.7遍历子节点
for (int j = 0; j < childList.getLength(); j++) {
// 5.8 声明book子节点
Node child = childList.item(j);
// 判断是否是节点。NodeType有三种:
// Element(节点):1,Attribute(属性):2,#Text(文本):3。
if (Node.ELEMENT_NODE == child.getNodeType()) {
// 5.9使用switch case判断,并为book对象属性赋值
switch (child.getNodeName()) {
case "title":
book.setTitle(child.getTextContent());
break;
case "author":
book.setAuthor(child.getTextContent());
break;
case "price":
book.setPrice(Double.valueOf(child.getTextContent()));
break;
case "year":
book.setYear(child.getTextContent());
break;
case "language":
book.setLanguage(child.getTextContent());
break;
default:
System.out.println("未知属性!");
}
}
}
// 6 将book对象放入集合
bookList.add(book);
}
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 生成XML文件,写入book列表
*
* @param file
*/
private void createXML(List<Book> bookList, String file, String encoding) {
DocumentBuilder builder = getDocumentBuilder();
//创建document文件
Document document = builder.newDocument();
// 在document中,创建根节点
Element eBookstore = document.createElement("bookstore");
for (Book book : bookList) {
// 创建book节点
Element eBook = document.createElement("book");
// 为book节点添加id属性
eBook.setAttribute("id", String.valueOf(book.getId()));
// 创建book的子节点
Element eTitle = document.createElement("title");
// 创建CDATA元素
CDATASection titleByCDATA = document.createCDATASection(book.getTitle());
// 将CDATA元素加入title
eTitle.appendChild(titleByCDATA);
eTitle.setTextContent(book.getTitle());
Element eAuthor = document.createElement("author");
eAuthor.setTextContent(book.getAuthor());
Element eYear = document.createElement("year");
eYear.setTextContent(book.getYear());
Element ePrice = document.createElement("price");
ePrice.setTextContent(String.valueOf(book.getPrice()));
Element eLanguage = document.createElement("language");
eLanguage.setTextContent(book.getLanguage());
// 将子节点添加到book中
eBook.appendChild(eTitle);
eBook.appendChild(eAuthor);
eBook.appendChild(eYear);
eBook.appendChild(ePrice);
eBook.appendChild(eLanguage);
// 将book节点添加到bookstore根节点中
eBookstore.appendChild(eBook);
}
// 将bookstore节点添加到document中,目前文件只存在与内存中
document.appendChild(eBookstore);
// 创建TransformerFactory 对象
Transformer former = getTransformer(encoding);
try {
// 创建输出流
OutputStream output = new FileOutputStream(file);
// 输出XML文件到硬盘
former.transform(new DOMSource(document), new StreamResult(output));
} catch (TransformerConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 获取Tranformer对象
*
* @param encoding
* 指定编码
* @return Tranformer
*/
private Transformer getTransformer(String encoding) {
// 创建TransformerFactory 对象
TransformerFactory factory = TransformerFactory.newInstance();
Transformer former = null;
try {
former = factory.newTransformer();
// 换行
former.setOutputProperty(OutputKeys.INDENT, "yes");
// 指定编码
former.setOutputProperty(OutputKeys.ENCODING, encoding);
} catch (TransformerConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return former;
}
标签:style end 文件解析 bre efault ase attr ring 硬盘
原文地址:http://www.cnblogs.com/Maple-leaves/p/6255490.html