List of usage examples for org.w3c.dom Node getOwnerDocument
public Document getOwnerDocument();
Document
object associated with this node. From source file:Main.java
public static Node appendTextNode(Node parent, String child) { if (parent == null) System.out.println("parent null 0"); if (parent instanceof Document) parent = ((Document) parent).getDocumentElement(); if (parent == null) System.out.println("parent null 1"); if (parent.getOwnerDocument() == null) System.out.println("parent ownerdoc null"); if (child == null) System.out.println("child null"); Text newNode = parent.getOwnerDocument().createTextNode(child); return parent.appendChild(newNode); }
From source file:DomUtil.java
public static void setAttribute(Node node, String attName, String val) { NamedNodeMap attributes = node.getAttributes(); Node attNode = node.getOwnerDocument().createAttribute(attName); attNode.setNodeValue(val); attributes.setNamedItem(attNode);// w w w .j a v a 2 s. c om }
From source file:Main.java
public static String printXMLNode(Node node) { try {/*from ww w. j av a 2 s . c o m*/ TransformerFactory tf = TransformerFactory.newInstance(); tf.setAttribute("indent-number", 2); Transformer transformer = tf.newTransformer(); String omitDeclaration = node instanceof Document || node == node.getOwnerDocument().getDocumentElement() ? "no" : "yes"; transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitDeclaration); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(node), new StreamResult(writer)); String output = writer.getBuffer().toString(); output = removeEmptyLines(output); // .replaceAll("\n|\r", ""); return output; } catch (TransformerException te) { throw new RuntimeException(te); } }
From source file:Main.java
public static TreeWalker createTreeWalker(Node node, int whatToShow, NodeFilter nodeFilter, boolean entityReferenceExpansion) { final DocumentTraversal documentTraversal; if (node instanceof Document) { documentTraversal = (DocumentTraversal) node; } else {/*from ww w . j a v a2 s.c o m*/ documentTraversal = (DocumentTraversal) node.getOwnerDocument(); } return documentTraversal.createTreeWalker(node, whatToShow, nodeFilter, entityReferenceExpansion); }
From source file:XMLUtils.java
public static Element createElementNS(Node node, QName name) { return createElementNS(node.getOwnerDocument(), name.getNamespaceURI(), name.getLocalPart()); }
From source file:XMLUtils.java
public static Text createTextNode(Node node, String data) { return createTextNode(node.getOwnerDocument(), data); }
From source file:DomUtil.java
/** * Set or replace the text value// ww w. j a v a 2s . c o m */ public static void setText(Node node, String val) { Node chld = DomUtil.getChild(node, Node.TEXT_NODE); if (chld == null) { Node textN = node.getOwnerDocument().createTextNode(val); node.appendChild(textN); return; } // change the value chld.setNodeValue(val); }
From source file:DOMUtil.java
/** * Automatically set text in a Node. Basically we find the first * Text node beneath the current node and replace it with a * CDATASection for the incoming text. All other Text nodes are * removed. Throws a DOMException if it's illegal to add a Text * child to the particular node./*from ww w .ja v a 2s. c o m*/ * * @param node the starting node for the search. * @param text the text to be set * @param allowMarkupInText whether to allow markup in text to pass through unparsed * @return the updated node * @throws DOMException if the Text object is not found */ public static Node setTextInNode(Node node, String text, boolean allowMarkupInText) { //start by setting the value in the first text node we find with a comment Comment comment = node.getOwnerDocument().createComment(""); Node newNode = null; //csc_092701.1 - support both encoded/unencoded text if (allowMarkupInText) newNode = node.getOwnerDocument().createCDATASection(text); else newNode = node.getOwnerDocument().createTextNode(text); //System.out.println ("newNode: "+newNode); Text textComp = DOMUtil.findFirstText((Element) node); //System.out.println ("textComp:"+textComp); if (textComp == null) { node.appendChild(comment); } else { Node parent = textComp.getParentNode(); parent.replaceChild(comment, textComp); } //now remove all the rest of the text nodes removeAllTextNodes(node); //now replace the comment with the newNode Node parent = comment.getParentNode(); parent.replaceChild(newNode, comment); //System.out.println ("parent: "+parent); //System.out.println ("result: "+DOMUtil.findFirstText((Element) parent)); //DOMUtil.printStackTrace(parent.getOwnerDocument().getDocumentElement()); return node; }
From source file:Main.java
/** * Adds a new child element to a parent. * //from w ww. j a v a 2 s . co m * @param parent the Element to which to append the child * @param name the (tag) name of the new child * @param value the text value of the new element. (can be null!) * @return the new child element */ static public Element addElement(Node parent, String name, String value) { if (parent == null) return null; // Fehler // Name must not contain spaces if (name.indexOf(' ') >= 0) name = name.replace(' ', '_'); // Create Element Element child = parent.getOwnerDocument().createElement(name); if (value != null) setElementText(child, value); parent.appendChild(child); return child; }
From source file:Main.java
public static void writeOutAttributesForNode(String[][] attributes, Node node) { if (attributes != null) { // Add attributes for (int n = 0; n < attributes.length; n++) { String key = attributes[n][0]; String value = attributes[n][1]; if ((key != null) && (value != null)) { Attr attNode = node.getOwnerDocument().createAttribute(key.toString()); attNode.setNodeValue(value.toString()); node.getAttributes().setNamedItem(attNode); }/*from ww w . j ava2s . c om*/ } } }