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 void setTextContent(Node node, String nodeValue) { if (node == null || nodeValue == null) throw new NullPointerException(); node.appendChild(node.getOwnerDocument().createTextNode(xmlEncode(nodeValue))); }
From source file:Main.java
/** * The method creates an element with specified name and attributes and appends it to the parent element. * This is a convenience method for often used sequence of calls. * * @param parent - the node where to append the new element. * @param name - the name of the element type being created. * @param attributes - string array of pairs attributes and their values. * Each attribute must have a value, so the array must have even number of elements. * @return the newly created element.//from w w w. j a va 2 s. c om * * @throws ArrayIndexOutOfBoundsException in case of odd number of elements of the attribute array * (i.e. the last attribute is missing a value). */ public static Element appendElement(Node parent, String name, String[] attributes) { Document doc = parent instanceof Document ? (Document) parent : parent.getOwnerDocument(); Element element = doc.createElement(name); if (attributes != null) { int attrLen = attributes.length; for (int i = 0; i < attrLen; i += 2) { String attrName = attributes[i]; String attrValue = attributes[i + 1]; element.setAttribute(attrName, attrValue); } } parent.appendChild(element); return element; }
From source file:Main.java
public static void appendChild(Node root, String name, String value) { appendChild(root.getOwnerDocument(), root, name, value); return;// w w w. j av a 2s . c om }
From source file:Main.java
/** * to get node's owner document/*from w w w. j a v a 2 s . c om*/ * * @return The document that contain node */ public static Document getOwnerDocument(Node node) { Document doc = null; if (node.getNodeType() == Node.DOCUMENT_NODE) { doc = (Document) node; } else { doc = node.getOwnerDocument(); } return doc; }
From source file:Main.java
private static Node convertFromNamespaceForm(final Node node) { if (node instanceof Element) { final Document document = node.getOwnerDocument(); final Element newElement = document.createElementNS(null, node.getLocalName()); final NodeList children = node.getChildNodes(); for (int i = 0, n = children.getLength(); i < n; i++) { final Node oldChildNode = children.item(i); final Node newChildNode = convertFromNamespaceForm(oldChildNode); newElement.appendChild(newChildNode); }//w w w .j a v a 2 s . com final NamedNodeMap attributes = node.getAttributes(); for (int i = 0, n = attributes.getLength(); i < n; i++) { final Attr attr = (Attr) attributes.item(i); final String attrQualifiedName = attr.getNodeName(); final String attrLocalName = attr.getLocalName(); if (!attrQualifiedName.equals(XMLNS) && !attrQualifiedName.startsWith(XMLNS_COLON) && !attrLocalName.equals(XSI_SCHEMA_LOCATION_ATTR)) { newElement.setAttributeNS(null, attrLocalName, attr.getValue()); } } return newElement; } else { return node.cloneNode(true); } }
From source file:Main.java
/** * Copy one node to another node./*from w w w . j av a2 s.c o m*/ * @param source source Node * @param dest destination Node * @return destination Node */ public static synchronized Node copyNode(Node source, Node dest) { if (source.getNodeType() == Node.TEXT_NODE) { Text tn = dest.getOwnerDocument().createTextNode(source.getNodeValue()); return tn; } Node attr = null; NamedNodeMap attrs = source.getAttributes(); if (attrs != null) { for (int i = 0; i < attrs.getLength(); i++) { attr = attrs.item(i); ((Element) dest).setAttribute(attr.getNodeName(), attr.getNodeValue()); } } Node child = null; NodeList list = source.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { child = list.item(i); if (!(child instanceof Text)) { Element en = dest.getOwnerDocument().createElementNS(child.getNamespaceURI(), child.getNodeName()); if (child.getNodeValue() != null) { en.setNodeValue(child.getNodeValue()); } Node n = copyNode(child, en); dest.appendChild(n); } else if (child instanceof CDATASection) { CDATASection cd = dest.getOwnerDocument().createCDATASection(child.getNodeValue()); dest.appendChild(cd); } else { Text tn = dest.getOwnerDocument().createTextNode(child.getNodeValue()); dest.appendChild(tn); } } return dest; }
From source file:Main.java
public static Document getOwnerDocument(Node node) { if (node.getNodeType() == Node.DOCUMENT_NODE) { return (Document) node; } else {/* ww w . j a v a 2s . c o m*/ return node.getOwnerDocument(); } }
From source file:Main.java
public static void spreadNamespaces(Node node, String tns, boolean overwrite) { Document doc = node instanceof Document ? (Document) node : node.getOwnerDocument(); boolean isParent = false; while (node != null) { Node next = null;//from w ww . j a v a 2 s . c o m if (!isParent && node.getNodeType() == Node.ELEMENT_NODE) { if (node.getNamespaceURI() == null) { node = doc.renameNode(node, tns, node.getNodeName()); } else { if (overwrite) { tns = node.getNamespaceURI(); } } NamedNodeMap nodeMap = node.getAttributes(); int nodeMapLengthl = nodeMap.getLength(); for (int i = 0; i < nodeMapLengthl; i++) { Node attr = nodeMap.item(i); if (attr.getNamespaceURI() == null) { doc.renameNode(attr, tns, attr.getNodeName()); } } } isParent = (isParent || (next = node.getFirstChild()) == null) && (next = node.getNextSibling()) == null; node = isParent ? node.getParentNode() : next; if (isParent && node != null) { if (overwrite) { tns = node.getNamespaceURI(); } } } }
From source file:Main.java
public static void appendChild(Node root, String name, String nsURI, String value) { appendChild(root.getOwnerDocument(), root, nsURI, name, value); return;//from www .ja v a2s .c o m }
From source file:Main.java
public static String getXPathForElement(Node e, NamespaceContext ctx) { StringBuffer sb = new StringBuffer(); List<Node> path = new ArrayList<Node>(); Node currentNode = e; while (currentNode.getParentNode() != currentNode.getOwnerDocument()) { path.add(0, currentNode);/*from w ww . j a v a2 s.c o m*/ if (currentNode instanceof Attr) { Attr a = (Attr) currentNode; currentNode = a.getOwnerElement(); } else { currentNode = currentNode.getParentNode(); } } path.add(0, currentNode); // We need the root element for (Node n : path) { sb.append("/"); if (n.getNodeType() == Node.ATTRIBUTE_NODE) { sb.append("@"); } String namespaceURI = n.getNamespaceURI(); if (namespaceURI != null && !namespaceURI.equals("")) { sb.append(ctx.getPrefix(namespaceURI)).append(":"); } sb.append(n.getLocalName()); if (n.getNodeType() == Node.ELEMENT_NODE) { appendElementQualifier(sb, (Element) n); } } return sb.toString(); }