dom4j-cookbook
【1】讀取XML 數據 1)intro:dom4j 附帶了一組builder 類用于解析xml 數據和創建 類似于樹的對象結構。讀取XML 數據的代碼如下: public class DeployFileLoaderSample {/** dom4j object model representation of a xml document. Note: We use the interface(!) not its implementation */private Document doc;/*** Loads a document from a file.* @param aFile the data source* @throw a org.dom4j.DocumentExcepiton occurs on parsing failure.*/public void parseWithSAX(File aFile) throws DocumentException {SAXReader xmlReader = new SAXReader();this.doc = xmlReader.read(aFile);}/*** Loads a document from a file.* @param aURL the data source* @throw a org.dom4j.DocumentExcepiton occurs on parsing failure.*/public void parseWithSAX(URL aURL) throws DocumentException {SAXReader xmlReader = new SAXReader();this.doc = xmlReader.read(aURL);}public Document getDoc() {return doc;} } 2)以上代碼 闡明了使用 SAXReader根據給定文件 來創建一個完整dom4j 樹。org.dom4j.io 包 包含了一組類用于創建和序列化XML對象。其中read() 方法被重載了使得你能夠傳遞表示不同資源的對象; java.lang.String - a SystemId is a String that contains a URI e.g. a URL to a XML file java.net.URL - represents a Uniform Resource Loader or a Uniform Resource Identifier. Encapsulates a URL. java.io.InputStream - an open input stream that transports xml data java.io.Reader - more compatible. Has abilitiy to specify encoding scheme org.sax.InputSource - a single input source for a XML entity.
2.1)添加新方法為 為?DeployFileCreator? 增加更多的擴展性,代碼還是上面那個代碼;
3)測試用例如下
【2】dom4j 和 其他XML API 整合
1)intro:dom4j 也提供了類用于和兩個原始 XML 處理API(SAX 和 DOM) 進行整合。
2)DomReader類: 允許你將一個存在的 DOM 樹 轉換為 dom4j 樹。你也可以 轉換一個DOM 文檔,DOM 節點分支 和 單個元素;代碼如下:
public class DOMIntegratorSample {public DOMIntegratorSample() {}public org.w3c.dom.Document parse(URL url) {DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();try {DocumentBuilder builder = factory.newDocumentBuilder();return builder.parse(url.toString());} catch (Exception e) {e.printStackTrace();return null;}}/** converts a W3C DOM document into a dom4j document */public Document buildDocment(org.w3c.dom.Document domDocument) {DOMReader xmlReader = new DOMReader();return xmlReader.read(domDocument);} }public String base = System.getProperty("user.dir") + File.separator+ "src" + File.separator;@Test // 測試用例,.public void testIntegrate() {DOMIntegratorSample sample = new DOMIntegratorSample();try {org.w3c.dom.Document doc = sample.parse(new URL("file:"+ base + "pom.xml"));Document doc4j = sample.buildDocment(doc);System.out.println(doc4j.asXML());} catch (Exception e) {e.printStackTrace();}}
【3】DocumentFactory 的秘密
1)intro:?從頭到尾創建一個 Document,代碼如下:
public class GranuatedDeployFileCreator {private DocumentFactory factory;private Document doc;public GranuatedDeployFileCreator() {this.factory = DocumentFactory.getInstance(); // 單例方法.}public void generateDoc(String aRootElement) {doc = factory.createDocument();Element root = doc.addElement(aRootElement);} }
1.1)測試用例如下:
@Testpublic void testGenerateDoc() {GranuatedDeployFileCreator creator = new GranuatedDeployFileCreator();creator.generateDoc("project");Document doc = creator.getDoc();System.out.println(doc.asXML());}
2)Document 和 Element 接口有許多 助手方法以簡單的方式來創動態建 XML 文檔;
public class Foo {public Foo() {}public Document createDocument() {Document document = DocumentHelper.createDocument();Element root = document.addElement("root");Element author2 = root.addElement("author").addAttribute("name", "Toby").addAttribute("location", "Germany").addText("Tobias Rademacher");Element author1 = root.addElement("author").addAttribute("name", "James").addAttribute("location", "UK").addText("James Strachan");return document;} }
2.1)測試用例如下:
@Testpublic void testCreateDocByHelper() {Foo foo = new Foo();Document doc = foo.createDocument();System.out.println(doc.asXML());}
2.2)dom4j 是基于API 的接口。這意味著dom4j中的 ?DocumentFactory 和 閱讀器類 總是使用 org.dom4j 接口而不是其實現類。 集合 API 和 W3C 的DOM 也采用了這種 方式;
2.3)一旦你解析后創建了一個文檔,你就想要將其序列化到硬盤或普通流中。dom4j 提供了一組類以以下四種方式 來序列化 你的 dom4j 樹;?XML + HTML + DOM + SAX Events;
【4】序列化到XML
1)intro: 使用 XMLWriter 構造器根據給定的字符編碼 來傳遞 輸出流。相比于輸出流,Writer 更容易使用,因為Writer 是基于字符串的,因此有很少的編碼問題。Writer.write()方法 被重寫了,你可以按需逐個寫出dom4j對象;
2)代碼如下:
// 序列化xml public class DeployFileCreator3 {private Document doc;public DeployFileCreator3(Document doc) {this.doc = doc;}public void serializetoXML(OutputStream out, String aEncodingScheme) throws Exception {OutputFormat outformat = OutputFormat.createPrettyPrint();outformat.setEncoding(aEncodingScheme);XMLWriter writer = new XMLWriter(out, outformat);writer.write(this.doc);writer.flush();writer.close();}}
3)測試用例
@Testpublic void testSerializetoXML() {Foo foo = new Foo();Document doc = foo.createDocument();DeployFileCreator3 creator = new DeployFileCreator3(doc);try {creator.serializetoXML(new FileOutputStream(base + "serializable.xml"),"UTF-8");System.out.println("serializable successfully.");} catch (Exception e) {e.printStackTrace();}}
<?xml version="1.0" encoding="UTF-8"?> <root><author name="Toby" location="Germany">Tobias Rademacher</author><author name="James" location="UK">James Strachan</author> </root>
【4.1】自定義輸出格式?
1)intro:即是說,你可以定義xml的輸出格式(aEncodingScheme)
// customize output format. public class DeployFileCreator4 {private Document doc;private OutputFormat outFormat;public DeployFileCreator4(Document doc) {this.outFormat = OutputFormat.createPrettyPrint();this.doc = doc;}public DeployFileCreator4(Document doc, OutputFormat outFormat) {this.doc = doc;this.outFormat = outFormat;}public void writeAsXML(OutputStream out) throws Exception {XMLWriter writer = new XMLWriter(out, this.outFormat);writer.write(this.doc);}public void writeAsXML(OutputStream out, String encoding) throws Exception {this.outFormat.setEncoding(encoding);this.writeAsXML(out);} }
2)OutputFormat中一個有趣的特性是 能夠設置字符編碼。使用這種機制設置XMLWriter的編碼方式是一個好習慣,使用這種編碼方式創建OutputStream 和 輸出XML的聲明。
3)測試用例:
@Testpublic void testCustomizeOutputFormat() {Foo foo = new Foo();Document doc = foo.createDocument();OutputFormat format = OutputFormat.createCompactFormat();format.setEncoding("UTF-8");DeployFileCreator4 creator = new DeployFileCreator4(doc, format);try {creator.writeAsXML(new FileOutputStream(base + "customizeFormat.xml"));System.out.println("successful customize format");} catch (Exception e) {e.printStackTrace();}}<?xml version="1.0" encoding="UTF-8"?> <root><author name="Toby" location="Germany">Tobias Rademacher</author><author name="James" location="UK">James Strachan</author></root>
【5】打印HTML
1)intro:HTMLWriter 帶有一個dom4j 樹 且會將該樹 格式化為 HMTL流。這個格式化器 類似于 XMLWriter 但輸出的是 CDATA 和 實體區域而不是 序列化格式的XML,且它支持許多沒有結束標簽的HTML 元素。如<br>;
2)代碼如下:
public class PrintHTML {private Document doc;private OutputFormat outFormat;public PrintHTML(Document doc) {this.outFormat = OutputFormat.createPrettyPrint();this.doc = doc;}public PrintHTML(Document doc, OutputFormat outFormat) {this.doc = doc;this.outFormat = outFormat;}public void writeAsHTML(OutputStream out) throws Exception {HTMLWriter writer = new HTMLWriter(out, this.outFormat);writer.write(this.doc);writer.flush();} }
3)測試用例:
@Testpublic void testPrintHTML() {Foo foo = new Foo();Document doc = foo.createDocument();PrintHTML creator = new PrintHTML(doc);try {creator.writeAsHTML(new FileOutputStream(base + "printHtml.html"));System.out.println("PrintHTML successfully");} catch (Exception e) {e.printStackTrace();}}
總結
以上是生活随笔為你收集整理的dom4j-cookbook的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电脑主板驱动程序无法修复(电脑主板驱动程
- 下一篇: java_basic_review(5)