@Override public Document getAsDocument() { try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document document = db.newDocument(); appendElement(document); return document; } catch (ParserConfigurationException pce) { throw new RuntimeException(pce); } }
protected void appendElement(org.w3c.dom.Node parentNode) { DocumentNavigator docNav = new DocumentNavigator(); Document ownerDocument = parentNode.getOwnerDocument(); if (ownerDocument == null) { // If the parentNode is a Document itself, it's ownerDocument is // null ownerDocument = (Document) parentNode; } String elementName = docNav.getElementName(this); Element element = ownerDocument.createElement(elementName); parentNode.appendChild(element); for (Iterator<Attribute> iter = docNav.getAttributeAxisIterator(this); iter.hasNext();) { Attribute attr = iter.next(); element.setAttribute(attr.getName(), attr.getStringValue()); } for (Iterator<Node> iter = docNav.getChildAxisIterator(this); iter.hasNext();) { AbstractNode child = (AbstractNode) iter.next(); child.appendElement(element); } }
@Override public Document getAsDocument() { try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document document = db.newDocument(); appendElement(document); return document; } catch (ParserConfigurationException pce) { throw new RuntimeException(pce); } }
protected void appendElement(org.w3c.dom.Node parentNode) { DocumentNavigator docNav = new DocumentNavigator(); Document ownerDocument = parentNode.getOwnerDocument(); if (ownerDocument == null) { // If the parentNode is a Document itself, it's ownerDocument is // null ownerDocument = (Document) parentNode; } String elementName = docNav.getElementName(this); Element element = ownerDocument.createElement(elementName); parentNode.appendChild(element); for (Iterator<Attribute> iter = docNav.getAttributeAxisIterator(this); iter.hasNext();) { Attribute attr = iter.next(); element.setAttribute(attr.getName(), attr.getStringValue()); } for (Iterator<Node> iter = docNav.getChildAxisIterator(this); iter.hasNext();) { AbstractNode child = (AbstractNode) iter.next(); child.appendElement(element); } }