mozdom4java
| resources: | Home Download Sample code Source Code Mailing List Bugs Members |
|---|
Sample code
For using the mozdom4java connector you need to obtain from Mozilla an instance of the nsIDOMNode class, for example nsIDOMDocument. Then our code can be used to wrap that Mozilla node into a standard W3C DOM node.
The easiest way is to use the SWT/Browser widget from the Eclipse/ATF project. The whole ATF project is not needed, the browser widget can be easily separated and used in a standalone SWT application. The MozillaBrowser class has a method called getDocument() that returns the currently loaded nsIDOMDocument.
To avoid SWT, you can implement a mini Mozilla embedding application as described in JavaXPCOM documentation. Then a nsIDOMDocument instance can be obtained from nsIWebNavigation.
Once a nsIDOMNode instance is available the following code constructs are applicable:
- Adding necessary imports:
import org.mozilla.xpcom.nsIDOMNode; import org.mozilla.xpcom.nsIDOMElement; import org.w3c.dom.Node; import org.w3c.dom.Element; import org.mozilla.dom.NodeFactory; import org.mozilla.dom.NodeImpl; - For converting a mozilla node into W3C node use the NodeFactory:
nsIDOMNode mozNode = ...; Node node = NodeFactory.getNodeInstance(mozNode); nsIDOMElement mozElem = ...; Element elem = (Element) NodeFactory.getNodeInstance(mozElem); - For extracting mozilla node from a W3C node cast the node back
to our NodeImpl object and get the JavaXPCOM instance:
Node node = ...; nsIDOMNode mozNode = ((NodeImpl) node).getInstance(); Node element = null; nsIDOMElement mozElement = (nsIDOMElement) ((NodeImpl) element).getInstance(). queryInterface(nsIDOMElement.NS_IDOMELEMENT_IID);