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> getChildElementsByName(Element root, String tagName) { List<Element> tags = new LinkedList<Element>(); NodeList nl = root.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node child = nl.item(i);/* www . j a v a2 s. c om*/ if (child.getNodeType() != Node.ELEMENT_NODE) { continue; } if (tagName.equals(child.getNodeName())) { tags.add((Element) child); } } return tags; }
From source file:Main.java
public static Element getFirstChildElement(Element parent) { if (parent == null) { return null; }/*www .ja v a 2 s .com*/ for (Node n = parent.getFirstChild(); n != null; n = n.getNextSibling()) { if (n.getNodeType() == Node.ELEMENT_NODE) { return (Element) n; } } return null; }
From source file:Main.java
public static Element getLastChildElement(Element elem) { if (elem == null) { return null; }/*from w ww . j ava 2s.c o m*/ for (Node n = elem.getLastChild(); n != null; n = n.getPreviousSibling()) { if (n.getNodeType() == Node.ELEMENT_NODE) { return (Element) n; } } return null; }
From source file:Main.java
/** * Returns the child element nodes of the passed node * @param node The node to return the element child nodes of * @return a [possibly empty] list of nodes */// ww w . j a va2s . co m public static List<Node> getElementChildNodes(final Node node) { return getChildNodes(node, Node.ELEMENT_NODE); }
From source file:Main.java
public static List<Node> getTextAndElementChildren(Node node) { List<Node> result = new LinkedList<Node>(); NodeList children = node.getChildNodes(); if (children == null) { return result; }//from w w w .j av a2 s . com for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if (Node.ELEMENT_NODE == child.getNodeType() || Node.TEXT_NODE == child.getNodeType()) { result.add(child); } } return result; }
From source file:Main.java
/** * Gets the index of the specified element amongst elements with the same * name//from w w w . ja v a 2 s . c o m * * @param element * the element to get for * @return the index of the element, will be >= 1 */ public static int getElementIndex(Node element) { int result = 1; Node elm = element.getPreviousSibling(); while (elm != null) { if (elm.getNodeType() == Node.ELEMENT_NODE && elm.getNodeName().equals(element.getNodeName())) result++; elm = elm.getPreviousSibling(); } return result; }
From source file:Main.java
private static void convert(Node toCopy, Node saveTo, Document doc) { Node newNode;//ww w . ja va 2s.c o m switch (toCopy.getNodeType()) { case Node.ELEMENT_NODE: Element newElement = doc.createElementNS(toCopy.getNamespaceURI(), toCopy.getNodeName()); newNode = newElement; Element baseElement = (Element) toCopy; NamedNodeMap children = baseElement.getAttributes(); for (int i = 0; i < children.getLength(); i++) { convertAttribute((Attr) children.item(i), newElement, doc); } break; case Node.TEXT_NODE: newNode = doc.createTextNode(toCopy.getTextContent()); break; default: newNode = null; } if (newNode != null) { NodeList children = toCopy.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { convert(children.item(i), newNode, doc); } saveTo.appendChild(newNode); } }
From source file:Main.java
/** * Gets the next sibling Element of the node, skipping any Text nodes such as whitespace. * //from www . j a va2 s . c om * @param n The sibling to start with * @return The next sibling Element of n, or null if none */ public static Element getNextSiblingElement(Node n) { Node sib = n.getNextSibling(); while (sib != null && sib.getNodeType() != Node.ELEMENT_NODE) { sib = sib.getNextSibling(); } if (sib != null) { return (Element) sib; } else { return null; } }
From source file:Main.java
/** * Get all the direct children elements of an element that have a specific * tag name./*from w w w . j a va2 s. c o m*/ * * @param parent * The parent element. * @param name * The tag name to match. * * @return A list of Element's. */ public static List<Element> getElements(final Element parent, final String name) { final LinkedList<Element> list = new LinkedList<Element>(); Node node = parent.getFirstChild(); while (node != null) { if (node.getNodeType() == Node.ELEMENT_NODE) { final Element element = (Element) node; if (element.getTagName().equals(name)) { list.add(element); } } node = node.getNextSibling(); } return list; }
From source file:Main.java
/** * Convenience method to copy the contents of the old {@link Node} into the * new one.//from w ww. ja v a 2s. c o m * * @param newDoc * @param newNode * @param oldParent */ public static void copyContents(Document newDoc, Node newNode, Node oldNode) { // FIXME we should be able to achieve this with much less code (e.g. // the code commented out) but for some reason there are // incompatibility issues being spat out by the tests. // final NodeList childNodes = oldNode.getChildNodes(); // for (int i = 0; i < childNodes.getLength(); i++) { // final Node child = newDoc.importNode(childNodes.item(i), true); // newNode.appendChild(child); // } final NodeList childs = oldNode.getChildNodes(); for (int i = 0; i < childs.getLength(); i++) { final Node child = childs.item(i); Element newElem = null; switch (child.getNodeType()) { case Node.ELEMENT_NODE: //Get all the attributes of an element in a map final NamedNodeMap attrs = child.getAttributes(); // Process each attribute newElem = newDoc.createElement(child.getNodeName()); for (int j = 0; j < attrs.getLength(); j++) { final Attr attr = (Attr) attrs.item(j); // add attribute name and value to the new element newElem.setAttribute(attr.getNodeName(), attr.getNodeValue()); } newNode.appendChild(newElem); break; case Node.TEXT_NODE: newNode.appendChild(newDoc.createTextNode(getString(child))); } copyContents(newDoc, newElem, child); } }