我是需要轉換爲XML建立與dom4j的W3C的文檔,並沒有關於如何做到這一點...轉換的dom4j的Docment W3C的文檔
4
A
回答
4
我假設你想從去任何想法:
org.dom4j.Document
要:
org.w3c.dom.Document
Document document = ...;
String text = document.asXML();
從上串一個JavaRanch example到文檔中:
public static Document stringToDom(String xmlSource)
throws SAXException, ParserConfigurationException, IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(new InputSource(new StringReader(xmlSource)));
}
合併2,你應該有你需要的東西。
6
如果您有大型文檔,您可能希望避免將文檔序列化爲文本以達到性能原因。在這種情況下,最好直接使用SAX事件來完成轉換:
private static final TransformerFactory transformerFactory =
TransformerFactory.newInstance();
public static org.w3c.dom.Document toW3c(org.dom4j.Document dom4jdoc)
throws TransformerException {
SAXSource source = new DocumentSource(dom4jdoc);
DOMResult result = new DOMResult();
Transformer transformer = transformerFactory.newTransformer();
transformer.transform(source, result);
return (org.w3c.dom.Document) result.getNode();
}
+0
非常感謝,這工作很好! – 2011-10-04 22:13:18
6
檢出DOMWriter。這適用於我:
import org.dom4j.DocumentHelper;
import org.dom4j.io.DOMWriter;
org.dom4j.Document dom4jDoc = DocumentHelper.createDocument();
org.w3c.dom.Document w3cDoc = new DOMWriter().write(dom4jDoc)
+0
完美的作品,謝謝 – Anand 2014-03-18 08:53:24
相關問題
- 1. 將Java w3c文檔轉換爲XMLStreamReader
- 2. 使用dom4j讀取時轉換文檔編碼
- 3. 無法將xml字符串轉換爲w3c文檔
- 4. 寫dom4j的文檔以XML的文件,逃脫「\ r \ n \」
- 5. W3C文檔類型錯誤?
- 6. dom4j XPath不工作解析xhtml文檔
- 7. 如何將dom4j xml轉換爲Swing Jtree?
- 8. 轉換一個XML文檔轉換成另一個XML文檔
- 9. 將iframe文檔轉換爲父文檔
- 10. 更改DOM4J的編碼文檔:UTF到ISO-8859-1(Java)
- 11. 轉換HTML文檔中的URL?
- 12. 用於文檔格式轉換的API
- 13. 將word文檔轉換爲pdf的vbscript
- 14. XSLT轉換XML文檔的格式
- 15. dom4j將文本轉換爲特殊字符
- 16. 添加圖像時,使用JavaScript轉換HTML文檔的文檔
- 17. W3C CSS Validator如何識別css3文檔?
- 18. DOM4J /文件
- 19. 與dom4j的
- 20. 在dom4j的
- 21. 使用dom4j的
- 22. 在DOM4J
- 23. 從scala的XML到w3c DOM的任何轉換?
- 24. Rails文檔轉換建議
- 25. 將文檔轉換爲Jpg
- 26. 轉換XML文檔爲String
- 27. 通過HTTP轉換文檔
- 28. XSLT轉換XHTML文檔
- 29. C#。文檔轉換爲PDF
- 30. 轉換Excel 2010文檔?
請提供更多的細節,你想做什麼,至少對我來說真的不清楚。 – MeBigFatGuy 2010-11-15 03:12:48