/**
*Book类
*/
package com.imooc.xmlText;
public class Book {
private String id;
private String name;
private String author;
private String year;
private String price;
private String language;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
}
package com.imooc.xmlText;
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;
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 java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class DOMTest1 {
public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException, TransformerException {
DocumentBuilderFactory documentBuilderFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder=documentBuilderFactory.newDocumentBuilder();
//解析xml文件
Document document=documentBuilder.parse("demo/books1.xml");
NodeList bookList= document.getElementsByTagName("book");
Book newBook=new Book();
List<Book> newBookList=new ArrayList<Book>();
System.out.println("共有"+bookList.getLength()+"本书");
for(int i=0;i<bookList.getLength();i++){
Node book=bookList.item(i);
NamedNodeMap attrs= book.getAttributes();
for(int j=0;j<attrs.getLength();j++){
Node attr=attrs.item(j);
newBook.setId(attr.getNodeValue());
System.out.println(attr.getNodeName()+":"+attr.getNodeValue());}
NodeList childList=book.getChildNodes();
for(int m=0;m<childList.getLength();m++){
Node child=childList.item(m);
if(child.getNodeType()==Node.ELEMENT_NODE){
switch(child.getNodeName()){
case "name":
newBook.setName(child.getTextContent());
break;
case "author":
newBook.setAuthor(child.getTextContent());
break;
case "year":
newBook.setYear(child.getTextContent());
break;
case "price":
newBook.setPrice(child.getTextContent());
break;
case "language":
newBook.setLanguage(child.getTextContent());
break;
}
System.out.println(child.getNodeName()+":"+child.getTextContent());}
}
newBookList.add(newBook);
newBook=new Book();
}
System.out.println("共有"+newBookList.size()+"本书");
for (Book book:newBookList) {
System.out.println("第"+book.getId()+"本书");
System.out.println("Name:"+book.getName());
System.out.println("Author:"+book.getAuthor());
System.out.println("Year:"+book.getYear());
System.out.println("Price:"+book.getPrice());
System.out.println("Language:"+book.getLanguage());
System.out.println("----finish----");
}
//生成xml文件
Document newDocument=documentBuilder.newDocument();
Element bookstore=newDocument.createElement("bookstore");
for(int i=0;i<newBookList.size();i++) {
Book bookValue=newBookList.get(i);
Element book = newDocument.createElement("book");
book.setAttribute("id",bookValue.getId() );
if(bookValue.getName()!=null&&!bookValue.getName().trim().equals("")){
Element name=newDocument.createElement("name");
name.setTextContent(bookValue.getName());
book.appendChild(name);}
if(bookValue.getAuthor()!=null&&!bookValue.getAuthor().trim().equals("")){
Element author=newDocument.createElement("author");
author.setTextContent(bookValue.getAuthor());
book.appendChild(author);}
if(bookValue.getYear()!=null&&!bookValue.getYear().trim().equals("")){
Element year=newDocument.createElement("year");
year.setTextContent(bookValue.getYear());
book.appendChild(year);}
if(bookValue.getPrice()!=null&&!bookValue.getPrice().trim().equals("")){
Element price=newDocument.createElement("price");
price.setTextContent(bookValue.getPrice());
book.appendChild(price);}
if(bookValue.getLanguage()!=null&&!bookValue.getLanguage().trim().equals("")){
Element language=newDocument.createElement("language");
language.setTextContent(bookValue.getLanguage());
book.appendChild(language);}
bookstore.appendChild(book);
}
newDocument.appendChild(bookstore);
TransformerFactory transformerFactory=TransformerFactory.newInstance();
Transformer transformer=transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT,"yes");
transformer.transform(new DOMSource(newDocument),new StreamResult(new File("demo1/books0.xml")));
}
}