views:

56

answers:

2

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

+1  A: 

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.

skaffman
@skaffman: I guess its the other way - DOMConverter converts w3c to XOM's.
HanuAthena
@HanuAthena: It does both. There's one method that does XOM->DOM
skaffman
Am sorry, am not able to figure out which method is that - http://www.xom.nu/apidocs/index.html?nu/xom/converters/DOMConverter.html
HanuAthena
@HanuAthena: It's the fourth method. http://www.xom.nu/apidocs/nu/xom/converters/DOMConverter.html "Translates a XOM nu.xom.Document object into an equivalent org.w3c.dom.Document object."
skaffman
A: 

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