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 ArrayList<Element> getElementsByTagName(Node node, String tagName) { ArrayList<Element> elements = new ArrayList<Element>(); for (int i = 0; i < node.getChildNodes().getLength(); i++) { Node n = node.getChildNodes().item(i); if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(tagName)) { elements.add((Element) n); }//from w w w . ja v a2s. co m } return elements; }
From source file:Main.java
public static Node getNextElementSibling(Node node) { node = node.getNextSibling();/*from w w w. java 2s . co m*/ while (node != null && node.getNodeType() != Node.ELEMENT_NODE) { node = node.getNextSibling(); } return node; }
From source file:Main.java
public static Element getFirstChild(Element e) { if (e == null) return null; Node n = e.getFirstChild();//from ww w .j av a 2 s .c o m while (n != null && n.getNodeType() != Node.ELEMENT_NODE) n = n.getNextSibling(); return (Element) n; }
From source file:Main.java
public static Element getFirstChildElement(final Node parent) { if (parent != null) { Node tmpResult = parent.getFirstChild(); if (tmpResult != null) { if (Node.ELEMENT_NODE == tmpResult.getNodeType()) { return (Element) tmpResult; } else { return getNextSiblingElement(tmpResult); }// ww w .j a v a2s. c o m } } return null; }
From source file:Main.java
@SuppressWarnings("fallthrough") private static void getSetRec(final Node rootNode, final Set<Node> result, final Node exclude, final boolean com) { if (rootNode == exclude) { return;/*from w ww .j a v a2s . co m*/ } switch (rootNode.getNodeType()) { case Node.ELEMENT_NODE: result.add(rootNode); Element el = (Element) rootNode; if (el.hasAttributes()) { NamedNodeMap nl = el.getAttributes(); for (int i = 0; i < nl.getLength(); i++) { result.add(nl.item(i)); } } //no return keep working case Node.DOCUMENT_NODE: for (Node r = rootNode.getFirstChild(); r != null; r = r.getNextSibling()) { if (r.getNodeType() == Node.TEXT_NODE) { result.add(r); while (r != null && r.getNodeType() == Node.TEXT_NODE) { r = r.getNextSibling(); } if (r == null) { return; } } getSetRec(r, result, exclude, com); } return; case Node.COMMENT_NODE: if (com) { result.add(rootNode); } return; case Node.DOCUMENT_TYPE_NODE: return; default: result.add(rootNode); } }
From source file:Main.java
/** * * Convenience method to transfer a node (and all of its children) from one * * DOM XML document to another.//from w ww . ja v a 2 s .c om * * * * Note: this method is recursive. * * * * @param current the current Element to append the transfer to * * @param target the target document for the transfer * * @param n the Node to transfer * * @return Element the current element. */ public static Element transferNode(Element current, Document target, Node n) { String name = n.getNodeName(); String value = n.getNodeValue(); short type = n.getNodeType(); if (type == Node.ELEMENT_NODE) { // create a new element for this node in the target document Element e = target.createElement(name); // move all the attributes over to the target document NamedNodeMap attrs = n.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { Node a = attrs.item(i); e.setAttribute(a.getNodeName(), a.getNodeValue()); } // get the children for this node NodeList children = n.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { // transfer each of the children to the new document transferNode(e, target, children.item(i)); } // append the node to the target document element current.appendChild(e); } else if (type == Node.TEXT_NODE) { Text text = target.createTextNode(value); current.appendChild(text); } return current; }
From source file:Main.java
public static List<Element> getChildElementsByTagName(Element parentElement, String childTag) { NodeList nodelist = parentElement.getChildNodes(); List<Element> nodes = new ArrayList<Element>(); for (int i = 0; i < nodelist.getLength(); i++) { Node temp = nodelist.item(i); if (temp.getNodeType() == Node.ELEMENT_NODE && temp.getNodeName().equals(childTag)) { nodes.add((Element) temp); }//from w ww. ja va 2 s .co m } return nodes; }
From source file:Main.java
/*************************************************************************** * Searches nodes in the given list for a child with a given name and value. * /* w w w .j a v a 2 s .c om*/ * @param list * @param childName * @param value * @return * @throws Exception **************************************************************************/ public static Node searchNodes(NodeList list, String childName, String value) throws Exception { for (int i = 0; i < list.getLength(); i++) { Node node = list.item(i); if (node.getNodeType() != Node.ELEMENT_NODE) continue; if (value.equals(getChildValueByName((Element) node, childName))) return node; } return null; }
From source file:Main.java
/** * Clones the given DOM node into the given DOM document. * // ww w . j a v a2 s . c om * @param node * The DOM node to clone. * @param doc * The target DOM document. * @return The cloned node in the target DOM document. */ public static Node cloneNode(Node node, Document doc) { Node clone = null; switch (node.getNodeType()) { case Node.ELEMENT_NODE: clone = doc.createElement(node.getNodeName()); NamedNodeMap attrs = node.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { Node attrNode = attrs.item(i); Attr attrClone = doc.createAttribute(attrNode.getNodeName()); attrClone.setNodeValue(attrNode.getNodeValue()); ((Element) clone).setAttributeNode(attrClone); } // Iterate through each child nodes. NodeList childNodes = node.getChildNodes(); if (childNodes != null) { for (int i = 0; i < childNodes.getLength(); i++) { Node childNode = childNodes.item(i); if (childNode == null) { continue; } Node childClone = cloneNode(childNode, doc); if (childClone == null) { continue; } clone.appendChild(childClone); } } break; case Node.TEXT_NODE: case Node.CDATA_SECTION_NODE: clone = doc.createTextNode(node.getNodeName()); clone.setNodeValue(node.getNodeValue()); break; } return clone; }
From source file:Main.java
public static Element findNextElement(Node current, boolean sameName) { String name = null;//from w w w . j a va 2 s .c o m if (sameName) { name = current.getNodeName(); } int type = Node.ELEMENT_NODE; return (Element) getNext(current, name, type); }