将 dom4j 文档转换为 W3c 文档

发布于 2024-10-02 14:49:43 字数 48 浏览 5 评论 0原文

我需要将使用 dom4j 构建的 XML 转换为 w3c 文档,但不知道如何操作。

I need to convert XML built with dom4j to a w3c document and don't have any idea about how do it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

此刻的回忆 2024-10-09 14:49:43

查看 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)

Check out DOMWriter. This works for me:

import org.dom4j.DocumentHelper;    
import org.dom4j.io.DOMWriter;

org.dom4j.Document dom4jDoc = DocumentHelper.createDocument();    
org.w3c.dom.Document w3cDoc = new DOMWriter().write(dom4jDoc)
╰沐子 2024-10-09 14:49:43

我假设您想要从:

org.dom4j.Document

到:

org.w3c.dom.Document

来自 dom4j 快速入门指南< /a>:

Document document = ...;
String text = document.asXML();

来自关于字符串到文档的 JavaRanch 示例

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 和你应该有你需要的东西。

I'm assuming you want to go from:

org.dom4j.Document

To:

org.w3c.dom.Document

From the dom4j quick start guide:

Document document = ...;
String text = document.asXML();

From a JavaRanch example on String to Document:

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)));
}

Combine the 2 and you should have what you need.

芯好空 2024-10-09 14:49:43

如果您有大型文档,出于性能原因,您可能希望避免将文档序列化为文本。在这种情况下,最好直接使用 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();
}

If you have large documents, you may want to avoid serializing your Document to text for performance reasons. In that case, it's best to use SAX events directly to do the transformation:

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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文