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
public static List<Element> elements(Element root, String tagName) { List<Element> L = new ArrayList<Element>(); if (root == null) return L; for (Node n1 = root.getFirstChild(); n1 != null; n1 = n1.getNextSibling()) { if (n1.getNodeType() != Node.ELEMENT_NODE) continue; Element e2 = Element.class.cast(n1); if (e2.getTagName().equals(tagName)) { L.add(e2);/*from w ww . j a va 2 s. co m*/ } } return L; }
From source file:Main.java
/*************************************************************************** * Sets the value of the first child under the given element with the given * name. If the child has no nodes, one is created. * /*from w ww .j a va 2s. c o m*/ * @param doc * @param e * @param name * @param value * @return * @throws Exception **************************************************************************/ public static boolean setChildValueByName(Document doc, Element e, String name, String value) throws Exception { NodeList childNodes = e.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node node = childNodes.item(i); if (node.getNodeType() != Node.ELEMENT_NODE) continue; if (node.getNodeName().equals(name)) { Node child = node.getFirstChild(); if (child == null) ((Element) node).appendChild(doc.createTextNode(value)); else child.setNodeValue(value); return true; } } return false; }
From source file:Main.java
public static List<Element> getSubChildElementList(Element ele, String[] tagnames) { if (ele == null) { return null; }//from w ww. ja va 2s. com List<Element> v = new ArrayList<Element>(); // boolean isall = false; NodeList tmpnl = ele.getChildNodes(); Node tmpn = null; int k; for (k = 0; k < tmpnl.getLength(); k++) { tmpn = tmpnl.item(k); if (tmpn.getNodeType() != Node.ELEMENT_NODE) { continue; } Element eee = (Element) tmpn; String noden = eee.getNodeName(); int p = noden.indexOf(':'); if (p >= 0) noden = noden.substring(p + 1); for (String tagname : tagnames) { if (tagname.equals(noden) || tagname.equals("*")) { v.add(eee); break; } } } return v; }
From source file:Main.java
private static String prettyPrintDom(Node node, String indent, boolean isRoot, boolean escapeStrings) { String ret = ""; switch (node.getNodeType()) { case Node.DOCUMENT_NODE: // recurse on each child NodeList nodes = node.getChildNodes(); if (nodes != null) { for (int i = 0; i < nodes.getLength(); i++) { ret += prettyPrintDom(nodes.item(i), indent, isRoot, escapeStrings); }// www. ja v a2 s . c om } break; case Node.ELEMENT_NODE: String name = node.getNodeName(); ret += indent + "<" + name; NamedNodeMap attributes = node.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { Node current = attributes.item(i); ret += " " + current.getNodeName() + "=\"" + ((escapeStrings) ? escapeStringForXML(current.getNodeValue()) : current.getNodeValue()) + "\""; } ret += ">"; // recurse on each child NodeList children = node.getChildNodes(); if (children != null) { for (int i = 0; i < children.getLength(); i++) { String tmp = prettyPrintDom(children.item(i), indent + ((isRoot) ? "" : BI), false, escapeStrings); if (!tmp.replaceAll("[\\s]+", "").equals("")) if (tmp.endsWith("\n")) if (ret.endsWith("\n")) ret += tmp; else ret += "\n" + tmp; else ret += tmp; } } if (ret.endsWith("\n")) ret += indent + "</" + name + ">\n"; else ret += "</" + name + ">\n"; break; case Node.TEXT_NODE: ret += (escapeStrings) ? escapeStringForXML(node.getNodeValue()) : node.getNodeValue(); break; case Node.COMMENT_NODE: ret += "<!-- " + node.getNodeValue() + " -->"; break; } return ret; }
From source file:Main.java
public static final String getComment(Element elem) { StringBuffer sb = new StringBuffer(); Node node = elem.getPreviousSibling(); while (node != null) { if (node.getNodeType() == Node.ELEMENT_NODE) { break; }//www. j a va2 s . c om if (node.getNodeType() == Node.COMMENT_NODE) { if (sb.length() > 0) { sb.insert(0, '\n'); sb.insert(0, ((Comment) node).getData()); } else { sb.append(((Comment) node).getData()); } } node = node.getPreviousSibling(); } return sb.toString(); }
From source file:Main.java
public static Element getNextSiblingElement(Element elem) { if (elem == null) { return null; }//www. j a v a 2s. co m for (Node n = elem.getNextSibling(); n != null; n = n.getNextSibling()) { if (n.getNodeType() == Node.ELEMENT_NODE) { return (Element) n; } } return null; }
From source file:Main.java
public static String getElementValue(final Node sourceNode) { String result = null;/*from w ww . j a v a 2 s . c om*/ if (sourceNode == null) { return result; } if (sourceNode.getNodeType() == Node.ELEMENT_NODE) { Node firstChild = sourceNode.getFirstChild(); if (firstChild != null) { result = firstChild.getNodeValue(); } } return result; }
From source file:Main.java
/** * @param element The element whose ancestry we will check * @param tagName The tagName of the element we are searching for in the ancestry * @param limitTagName Stop searching if we hit this limit * @return The first matching element, if found *//* w w w . j a va2 s .c om*/ public static Element getAncestorOrSelf(final Element element, final String tagName, final String limitTagName) { Element result = null; Element next = element; String currentTagName; do { currentTagName = next.getTagName(); if (currentTagName.equals(tagName)) { result = next; break; } else { Node parent = next.getParentNode(); if (parent != null && parent.getNodeType() == Node.ELEMENT_NODE) { next = (Element) parent; } else { break; } } } while (next != null && (limitTagName == null || !tagName.equals(limitTagName))); return result; }
From source file:Main.java
public static ArrayList<String> getNodeListAttValAsStringCol(String Xpath, Node node, String attrName) throws Exception { ArrayList<String> retV = new ArrayList<String>(); NodeList nl = getNodesListXpathNode(Xpath, node); int l = nl.getLength(); Element e = null;/*from ww w . ja va 2 s. c o m*/ String val = ""; for (int i = 0; i < l; i++) { e = (Element) nl.item(i); if (e.getNodeType() == Node.ELEMENT_NODE) { val = e.getAttribute(attrName); if (val != null && val.length() > 0) { //log.info("getNodeListAttValAsStringCol val = "+val +" attname = "+attrName); /*try { log.info(convertToStringLeaveCDATA(e)); }catch(Exception E) { E.printStackTrace(); }*/ retV.add(val); } } } return retV; }
From source file:Main.java
public static Element getPreviousSiblingElement(Element elem) { if (elem == null) { return null; }//from w w w. j a v a 2s . c o m for (Node n = elem.getPreviousSibling(); n != null; n = n.getPreviousSibling()) { if (n.getNodeType() == Node.ELEMENT_NODE) { return (Element) n; } } return null; }