List of usage examples for org.w3c.dom Node ELEMENT_NODE
short ELEMENT_NODE
To view the source code for org.w3c.dom Node ELEMENT_NODE.
Click Source Link
Element
. From source file:Main.java
/** * Whenever you'd need to print a configuration node and/or its children. * * @param root the root node to print.//from ww w. j av a 2 s. c om * @param out the print stream that should be used to outpu * @param recurse boolean * @param prefix String */ public static void printChildElements(Element root, PrintStream out, boolean recurse, String prefix) { out.print(prefix + "<" + root.getNodeName()); NamedNodeMap attrs = root.getAttributes(); Node node; for (int i = 0; i < attrs.getLength(); i++) { node = attrs.item(i); out.print(" " + node.getNodeName() + "=\"" + node.getNodeValue() + "\""); } out.println(">"); String data = getText(root); if (data != null && data.trim().length() > 0) out.println(prefix + "\t" + data); data = getCData(root); if (data != null && data.trim().length() > 0) out.println(prefix + "\t<![CDATA[" + data + "]]>"); NodeList nodes = root.getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { node = nodes.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { if (recurse) printChildElements((Element) node, out, recurse, prefix + "\t"); else out.println(prefix + node.getNodeName()); } } out.println(prefix + "</" + root.getNodeName() + ">"); }
From source file:Main.java
/** * Traverses through a DOM tree starting at the given element and removes all those text-nodes from it that only * contain white spaces./*w w w. ja v a 2 s . c om*/ * * @param parent * the parent Element */ public static void removeWhitespaceTextNodes(Element parent) { final NodeList nl = parent.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { final Node child = nl.item(i); if (child.getNodeType() == Node.TEXT_NODE) { if (child.getNodeValue().trim().length() == 0) { parent.removeChild(child); i--; // since the child is removed counting up must be made undone } } else if (child.getNodeType() == Node.ELEMENT_NODE && child.getChildNodes().getLength() > 0) { removeWhitespaceTextNodes((Element) child); } } }
From source file:Main.java
public static Element getFirst(NodeList nodes) { if (nodes == null || nodes.getLength() == 0) { return null; }//from w w w . ja v a 2 s.c o m Node node = nodes.item(0); if (node.getNodeType() != Node.ELEMENT_NODE) { return null; } return (Element) node; }
From source file:Main.java
public static int getCurrentPosition(Node refNode) { if (refNode == null) { return -1; }/*from ww w . j ava2 s . co m*/ int counter = 0; Node current = refNode; while (current != null) { if (current.getNodeType() == Node.ELEMENT_NODE) { counter++; } current = current.getPreviousSibling(); } return counter; }
From source file:Main.java
/** * Extracts child elements from node whith type <code>ELEMENT_NODE</code>. * * @param node root node of XML document for search. * @return iterator with proper node childs. *//*from w ww . j a v a2 s . co m*/ public static Iterator<Element> getChildElements(final Node node) { // node.normalize(); return new Iterator<Element>() { private final NodeList nodes = node.getChildNodes(); private int nextPos = 0; private Element nextElement = seekNext(); public boolean hasNext() { return nextElement != null; } public Element next() { if (nextElement == null) throw new NoSuchElementException(); final Element result = nextElement; nextElement = seekNext(); return result; } public void remove() { throw new UnsupportedOperationException("operation not supported"); } private Element seekNext() { for (int i = nextPos, len = nodes.getLength(); i < len; i++) { final Node childNode = nodes.item(i); if (childNode.getNodeType() == Node.ELEMENT_NODE) { nextPos = i + 1; return (Element) childNode; } } return null; } }; }
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;/* w w w .jav a 2 s .com*/ while (currentNode.getParentNode() != currentNode.getOwnerDocument()) { path.add(0, currentNode); 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(); }
From source file:Main.java
/** * //from w w w .j a v a 2 s . co m * @param context * @param element */ public static void recursiveIdBrowse(DOMValidateContext context, Element element) { for (int i = 0; i < element.getChildNodes().getLength(); i++) { Node node = element.getChildNodes().item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { Element childEl = (Element) node; String ID_ATTRIBUTE_NAME = "Id"; if (childEl.hasAttribute(ID_ATTRIBUTE_NAME)) { context.setIdAttributeNS(childEl, null, ID_ATTRIBUTE_NAME); } recursiveIdBrowse(context, childEl); } } }
From source file:Main.java
/** * Check whether the given nodes containes an attribute with the given name and value.<br> * The "matching" is assumed to be true even if the attribute just starts or ends with the given * attribute value.<br>/*w ww . j a va 2s. c o m*/ * This has been allowed to match multiple keyword attributes such as in * <code>class="hfeed hentry"</code> cases.<br> * * @param node node to be analyzed * @param attrName the attribute name to be matched * @param attrValue the attribute value to be matched * @return <code>true</code> if node containes the given attributes, <code>false</code> otherwise (even if node is null) */ public static boolean nodeAttributeMatches(Node node, String attrName, String attrValue) { if (node != null && node.getNodeType() == Node.ELEMENT_NODE) { String value = ((Element) node).getAttribute(attrName); return (value != null && value.length() > 0 && attributeValueMatches(value, attrValue)); } else { return false; } }
From source file:Main.java
/** * @return a list of all child node text contents with this name *//*from w w w. jav a2 s . c om*/ public static ArrayList<String> getChildNodesTextContents(Node node, String name) { ArrayList<String> results = new ArrayList<String>(); NodeList nodeList = node.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { Node temp = nodeList.item(i); if (temp.getNodeType() != Node.ELEMENT_NODE) continue; if (name.equals(temp.getLocalName()) || name.equals(temp.getNodeName())) { results.add(temp.getTextContent()); } } return (results); }
From source file:Main.java
/** Prints the specified node, then prints all of its children. */ public static void printDOM(Node node) { int type = node.getNodeType(); switch (type) { // print the document element case Node.DOCUMENT_NODE: { System.out.print("<?xml version=\"1.0\" ?>"); printDOM(((Document) node).getDocumentElement()); break;/*from w w w . jav a2 s . c om*/ } // print element with attributes case Node.ELEMENT_NODE: { System.out.println(); System.out.print("<"); System.out.print(node.getNodeName()); NamedNodeMap attrs = node.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { Node attr = attrs.item(i); System.out.print(" " + attr.getNodeName().trim() + "=\"" + attr.getNodeValue().trim() + "\""); } System.out.print(">"); NodeList children = node.getChildNodes(); if (children != null) { int len = children.getLength(); for (int i = 0; i < len; i++) printDOM(children.item(i)); } break; } // handle entity reference nodes case Node.ENTITY_REFERENCE_NODE: { System.out.print("&"); System.out.print(node.getNodeName().trim()); System.out.print(";"); break; } // print cdata sections case Node.CDATA_SECTION_NODE: { System.out.print("<![CDATA["); System.out.print(node.getNodeValue().trim()); System.out.print("]]>"); break; } // print text case Node.TEXT_NODE: { System.out.println(); System.out.print(node.getNodeValue().trim()); break; } // print processing instruction case Node.PROCESSING_INSTRUCTION_NODE: { System.out.print("<?"); System.out.print(node.getNodeName().trim()); String data = node.getNodeValue().trim(); { System.out.print(" "); System.out.print(data); } System.out.print("?>"); break; } } if (type == Node.ELEMENT_NODE) { System.out.println(); System.out.print("</"); System.out.print(node.getNodeName().trim()); System.out.print('>'); } }