将 nu.XOM.Element 转换为 org.w3c.dom.Element

发布于 2024-09-07 05:40:54 字数 162 浏览 4 评论 0原文

是否可以将 nu.XOM.Element 转换为 org.w3c.dom.Element ?

我正在尝试使用 XOM API 构建 XML。但我的旧版 API 很少需要 org.w3c.dom.Element。所以我只想知道我是否可以转换。

谢谢 :)

Is it possible to convert nu.XOM.Element to org.w3c.dom.Element?

Am trying to construct XML using XOM APIs. But few of my legacy APIs expects org.w3c.dom.Element. So, I just want to know if I can convert.

Thank You :)

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

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

发布评论

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

评论(3

玩世 2024-09-14 05:40:54

nu.xom.converters.DOMConverter 类,它提供了一种将整个 XOM 文档转换为相应 DOM 文档的方法,但您无法对单个元素执行此操作,可能是因为 W3C如果没有父 DocumentElement 就无法存在。

There is the nu.xom.converters.DOMConverter class, which provides a way of translating an entire XOM document into a corresponding DOM document, but you can't do it for individual elements, probably because a W3C Element can't exist without a parent Document.

强辩 2024-09-14 05:40:54

XOM 文档:

final nu.xom.Element root = new nu.xom.Element("root");
root.appendChild("Hello World!");
final nu.xom.Document xomDoc = new nu.xom.Document(root);

使用 DOMConverter:

final DocumentBuilderFactory factory = DocumentBuilderFactory
                .newInstance();
final DocumentBuilder builder = factory.newDocumentBuilder();
final DOMImplementation impl = builder.getDOMImplementation();
final Document w3cDoc= DOMConverter.convert(xomDoc, impl);

XOM Document:

final nu.xom.Element root = new nu.xom.Element("root");
root.appendChild("Hello World!");
final nu.xom.Document xomDoc = new nu.xom.Document(root);

using DOMConverter:

final DocumentBuilderFactory factory = DocumentBuilderFactory
                .newInstance();
final DocumentBuilder builder = factory.newDocumentBuilder();
final DOMImplementation impl = builder.getDOMImplementation();
final Document w3cDoc= DOMConverter.convert(xomDoc, impl);
淡笑忘祈一世凡恋 2024-09-14 05:40:54
public static org.w3c.dom.Document xomToDom(Element elem) {
    try {
        elem = (Element)elem.copy();
        return
        DOMConverter.convert(new Document(elem), 
                DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation());
    } catch (ParserConfigurationException e) {
        throw new RuntimeException(e);
    }
}
public static org.w3c.dom.Document xomToDom(Element elem) {
    try {
        elem = (Element)elem.copy();
        return
        DOMConverter.convert(new Document(elem), 
                DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation());
    } catch (ParserConfigurationException e) {
        throw new RuntimeException(e);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文