日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

dom4j-cookbook

發布時間:2023/12/3 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 dom4j-cookbook 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
【0】README 1)本文譯自http://dom4j.sourceforge.net/dom4j-1.6.1/cookbook.html? 2)intro:? 2.1)dom4j 是一個對象模型,在內存中表示一顆XML 樹。dom4j 提供了易于使用的API以提供強大的處理特性,操縱或控制 XML 和 結合 XPath, XSLT 以及 SAX, JAXP 和 DOM 來進行處理; 2.2)dom4j 是基于接口來設計的,來提供高可配置的實現策略。你只需提供一個DocumentFactory的實現就可以創建你自己的XML樹實現。這使得我們易于重用dom4j 的代碼,當擴展dom4j來提供所需特性的實現的時候;
【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)測試用例如下

@Testpublic void readXML() {String base = System.getProperty("user.dir") + File.separator+ "src" + File.separator;DeployFileLoaderSample sample = new DeployFileLoaderSample();try { // via parameter of URL type.sample.parseWithSAX(new URL("file:" + base + "pom.xml"));Document doc = sample.getDoc();System.out.println(doc.asXML());} catch (Exception e) {e.printStackTrace();}try { // via parameter of File type.sample.parseWithSAX(new File(base + "pom.xml"));Document doc = sample.getDoc();System.out.println(doc.asXML());} catch (Exception e) {e.printStackTrace();}}

【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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。