Java tutorial
//package com.java2s; import org.w3c.dom.*; public class Main { /** * This is a helper function to deal with problems that occur when importing Nodes from * JTidy Documents to Xerces Documents. */ public static Node importNode(Document d, Node n, boolean deep) { Node r = cloneNode(d, n); if (deep) { NodeList nl = n.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node n1 = importNode(d, nl.item(i), deep); r.appendChild(n1); } } return r; } public static Node cloneNode(Document d, Node n) { Node r = null; switch (n.getNodeType()) { case Node.TEXT_NODE: r = d.createTextNode(((Text) n).getData()); break; case Node.CDATA_SECTION_NODE: r = d.createCDATASection(((CDATASection) n).getData()); break; case Node.ELEMENT_NODE: r = d.createElement(((Element) n).getTagName()); NamedNodeMap map = n.getAttributes(); for (int i = 0; i < map.getLength(); i++) { ((Element) r).setAttribute(((Attr) map.item(i)).getName(), ((Attr) map.item(i)).getValue()); } break; } return r; } }