Question

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 :)

Was it helpful?

Solution

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.

OTHER TIPS

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);
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);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top