package SAX;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import handler.SaxParserHandler;
import javax.naming.spi.DirStateFactory.Result;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
import Entity.Book;
public class SaxTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
SaxTest st=new SaxTest();
// st.parseXML();
st.createXML();
}
public ArrayList<Book> parseXML(){
//获取一个SAXParserFactory的实例
SAXParserFactory factory=SAXParserFactory.newInstance();
SaxParserHandler handler=null;
try {
//通过factory获取一个saxpaser的实例
SAXParser parser=factory.newSAXParser();
//uri是路径 创建SAXhandler对象
handler=new SaxParserHandler();
parser.parse("新建文本文档.xml", handler);
// System.out.println(handler.getBooklist().size());
// for (Book book : handler.getBooklist()) {
// System.out.println(book.getAuthor());
// System.out.println("====完成====");
// }
} 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();
}
return handler.getBooklist();
}
public void createXML(){
ArrayList<Book> booklist =parseXML();
//生成xml
//创建一个SAXtransformerfactory对象
SAXTransformerFactory tff=(SAXTransformerFactory) SAXTransformerFactory.newInstance();
try {
//创建transforemerhandler对象。
TransformerHandler handler= tff.newTransformerHandler();
//创建transformer对象
Transformer tr= handler.getTransformer();
//通过transformer对xml文件进行设置
tr.setOutputProperty(OutputKeys.ENCODING, "utf-8"); //设置编码 必须在setresult之前
tr.setOutputProperty(OutputKeys.INDENT, "yes"); //设置换行
//创建result对象
File f= new File("src/res/books.xml");
if(!f.exists()){
try {
f.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//创建result对象并且使其与handler关联
javax.xml.transform.Result result=new StreamResult(new FileOutputStream(f));
handler.setResult(result);
//利用handler对xml文件进行内容的编写
//打开document
handler.startDocument();
AttributesImpl attr=new AttributesImpl();
//startelement是创建的节点 characters是创建节点的值
handler.startElement("", "", "bookstore", attr);
for (Book book : booklist) {
attr.clear();
attr.addAttribute("", "", "id", "", book.getId());
handler.startElement("", "", "book", attr);
attr.clear();
handler.startElement("", "", "name", attr);
handler.characters(book.getName().toCharArray(),0 ,book.getName().length() );
handler.endElement("", "", "name");
attr.clear();
handler.startElement("", "", "year", attr);
handler.characters(book.getYear().toCharArray(),0 ,book.getYear().length() );
handler.endElement("", "", "year");
attr.clear();
handler.startElement("", "", "author", attr);
handler.characters(book.getAuthor().toCharArray(),0 ,book.getAuthor().length() );
handler.endElement("", "", "author");
handler.endElement("", "", "book");
}
handler.endElement("", "", "bookstore");
//关闭document
handler.endDocument();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/*还报错了
com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 1 of 1-byte UTF-8 sequence.
at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.invalidByte(UTF8Reader.java:684)
at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.read(UTF8Reader.java:554)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(XMLEntityScanner.java:1742)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.peekChar(XMLEntityScanner.java:487)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2687)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
at javax.xml.parsers.SAXParser.parse(SAXParser.java:395)
at javax.xml.parsers.SAXParser.parse(SAXParser.java:277)
at SAX.SaxTest.parseXML(SaxTest.java:46)
at SAX.SaxTest.createXML(SaxTest.java:66)
at SAX.SaxTest.main(SaxTest.java:33)
*/