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
/** Finds and returns the first child node with the given name. */ public static Element getFirstChildElement(Node parent, String elemName) { if (parent == null) return null; // search for node Node child = parent.getFirstChild(); while (child != null) { if (child.getNodeType() == Node.ELEMENT_NODE) { if (child.getNodeName().equals(elemName)) { return (Element) child; }//from w w w . j ava 2 s . co m } child = child.getNextSibling(); } // not found return null; }
From source file:Main.java
public String getNodeType(Node node) { String type;// w w w . jav a 2s .co m switch (node.getNodeType()) { case Node.ELEMENT_NODE: { type = "Element"; break; } case Node.ATTRIBUTE_NODE: { type = "Attribute"; break; } case Node.TEXT_NODE: { type = "Text"; break; } case Node.CDATA_SECTION_NODE: { type = "CData section"; break; } case Node.ENTITY_REFERENCE_NODE: { type = "Entity reference"; break; } case Node.ENTITY_NODE: { type = "Entity"; break; } case Node.PROCESSING_INSTRUCTION_NODE: { type = "Processing instruction"; break; } case Node.COMMENT_NODE: { type = "Comment"; break; } case Node.DOCUMENT_NODE: { type = "Document"; break; } case Node.DOCUMENT_TYPE_NODE: { type = "Document type"; break; } case Node.DOCUMENT_FRAGMENT_NODE: { type = "Document fragment"; break; } case Node.NOTATION_NODE: { type = "Notation"; break; } default: { type = "???"; break; } } return type; }
From source file:Main.java
/** Finds and returns the next sibling node with the given name. */ public static Element getNextSiblingElement(Node node, String elemName) { if (node == null) return null; // search for node Node sibling = node.getNextSibling(); while (sibling != null) { if (sibling.getNodeType() == Node.ELEMENT_NODE) { if (sibling.getNodeName().equals(elemName)) { return (Element) sibling; }//from w w w .ja va2 s. co m } sibling = sibling.getNextSibling(); } // not found return null; }
From source file:Main.java
public static String getNodeValue(final Node node) { if (node.getNodeType() == Node.ELEMENT_NODE) { Node n = node.getFirstChild(); if (n != null) { do {//from www .j a va 2s.c o m if (n.getNodeType() == Node.TEXT_NODE) { return n.getNodeValue(); } } while ((n = n.getNextSibling()) != null); } } return node.getNodeValue(); }
From source file:Main.java
private static Object getElementValue(Node node) { NodeList nodes = node.getChildNodes(); int childCount = nodes.getLength(); int childElementCount = 0; for (int i = 0; i < childCount; i++) { Node child = nodes.item(i); if (child.getNodeType() == Node.ELEMENT_NODE) { childElementCount++;/*from w w w .java 2 s.c o m*/ } } if (childElementCount == 0) { return node.getTextContent(); } Map<String, Object> map = new LinkedHashMap<>(childElementCount); for (int i = 0; i < childCount; i++) { Node child = nodes.item(i); if (child.getNodeType() != Node.ELEMENT_NODE) { continue; } String childName = child.getNodeName(); Object childValue = child.hasChildNodes() ? toObject(child) : null; // auto detect repeating elements if (map.containsKey(childName)) { Object temp = map.get(childName); if (temp instanceof List) { List list = (List) temp; list.add(childValue); } else { List list = new ArrayList(childCount); map.put(childName, list); list.add(temp); list.add(childValue); } } else { map.put(childName, childValue); } } return map; }
From source file:Main.java
/** * Remove any whitespace text nodes from the DOM. Calling this before saving * a formatted document will fix the formatting indentation of elements * loaded from a different document./* w w w . j a v a2s .c o m*/ * * @param node * Node to remove whitespace nodes from * @param deep * Should this method recurse into the node's children? */ public static void removeWhitespace(Node node, boolean deep) { NodeList children = node.getChildNodes(); int length = children.getLength(); for (int i = 0; i < length; i++) { Node child = children.item(i); if (child.getNodeType() == Node.TEXT_NODE && length > 1) { Node previous = child.getPreviousSibling(); Node next = child.getNextSibling(); if ((previous == null || previous.getNodeType() == Node.ELEMENT_NODE || previous.getNodeType() == Node.COMMENT_NODE) && (next == null || next.getNodeType() == Node.ELEMENT_NODE || next.getNodeType() == Node.COMMENT_NODE)) { String content = child.getTextContent(); if (content.matches("\\s*")) //$NON-NLS-1$ { node.removeChild(child); i--; length--; } } } else if (deep && child.getNodeType() == Node.ELEMENT_NODE) { removeWhitespace(child, deep); } } }
From source file:com.seajas.search.utilities.web.WebPages.java
/** * Gets the textual content.//from w w w . j av a 2 s .c o m * * @param html the markup fragment. */ public static String getText(Node html) { if (html.getNodeType() == Node.ELEMENT_NODE) { logger.trace("Traveling element node"); StringBuilder text = new StringBuilder(); NodeList children = html.getChildNodes(); int count = children.getLength(); logger.debug(String.format("Searching for text in %d elements", count)); for (int i = 0; i < count; ++i) text.append(getText(children.item(i))); return text.toString(); } String result = html.getNodeValue(); return result == null ? "" : result; }
From source file:Main.java
/** * only returns the first instance of this child node * @param node/* w w w.j a va 2s . c o m*/ * @param localName * @return */ public static Node getChildNode(Node node, String name) { 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())) return (temp); } return (null); }
From source file:Main.java
public static String logElement(final Element elem, final int level) { String estr = ""; String indentText = " "; String addIndT = ""; // add indent int ind = 0;/* ww w . j a va 2 s . co m*/ while (ind < level) { addIndT = addIndT + indentText; ind++; } String name = elem.getNodeName(); estr = "\n" + addIndT + "<" + name + " "; // Attribs NamedNodeMap namedNodeMap = elem.getAttributes(); StringBuilder sb = new StringBuilder(estr); for (int i = 0; i < namedNodeMap.getLength(); i++) { Attr att = (Attr) namedNodeMap.item(i); sb.append(" " + estr + att.getName() + "=\"" + att.getNodeValue() + "\" "); } sb.append(">"); estr = sb.toString(); NodeList pl = elem.getChildNodes(); int index = pl.getLength(); // text nodes if (index > 0) { int i = 0; while (i < index) { Node domNode = pl.item(i); if ((domNode.getNodeType()) == org.w3c.dom.Node.TEXT_NODE) { String Etext = domNode.getNodeValue(); estr = estr + "\n " + addIndT + addIndT + Etext; } i++; } } // Child Elements if (index > 0) { int i = 0; while (i < index) { Node domNode = pl.item(i); if ((domNode.getNodeType()) == org.w3c.dom.Node.ELEMENT_NODE) { Element el = (Element) domNode; estr = estr + logElement(el, level + 1); } i++; } } estr = estr + "\n" + addIndT + "</" + name + ">"; return estr; }
From source file:Main.java
/** * Return the first child Element with the given name; if name is null returns the first element. *///from w w w . j a v a 2 s. co m public static Element firstChildElement(Element element, String childElementName, String attrName, String attrValue) { if (element == null) return null; // get the first element with the given name Node node = element.getFirstChild(); if (node != null) { do { if (node.getNodeType() == Node.ELEMENT_NODE && (childElementName == null || childElementName.equals(node.getNodeName()))) { Element childElement = (Element) node; String value = childElement.getAttribute(attrName); if (value != null && value.equals(attrValue)) { return childElement; } } } while ((node = node.getNextSibling()) != null); } return null; }