List of usage examples for org.w3c.dom Node getNodeType
public short getNodeType();
From source file:Main.java
static public ArrayList<Element> selectElements(Element element, String xpathExpression) throws XPathExpressionException { ArrayList<Element> resultVector = new ArrayList<Element>(); if (element == null) { return resultVector; }/* w w w.ja va2 s . co m*/ if (xpathExpression.indexOf("/") == -1) { NodeList nodeList = element.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(xpathExpression)) { resultVector.add((Element) node); } } } else { XPath xpath = XPathFactory.newInstance().newXPath(); NodeList nodes = (NodeList) xpath.evaluate(xpathExpression, element, XPathConstants.NODESET); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); resultVector.add((Element) node); } } return resultVector; }
From source file:Main.java
/** * Gets the text value contained in an element. * * @param node The Element to get the text value of. * * @return The text in the element./* ww w .j a v a 2s . c o m*/ */ public static String getTextValue(Element node) { if (node == null) { return null; } NodeList nodes = node.getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { Node n = nodes.item(i); if (n.getNodeType() == n.TEXT_NODE) { return n.getNodeValue(); } } return null; }
From source file:Main.java
/**gets the sibling element of a node * @param start the node to get the sibling of * @return the sibling node*//* w w w. j a v a 2 s . c o m*/ public static Node GetNextSiblingElement(Node start) { if (start == null) return start; Node node = start.getNextSibling(); if (node == null) return node; else if (node.getNodeType() != Node.ELEMENT_NODE) return GetNextSiblingElement(node); else return node; }
From source file:Main.java
public static String getPrefix(String uri, Node e) { while (e != null && (e.getNodeType() == Element.ELEMENT_NODE)) { NamedNodeMap attrs = e.getAttributes(); for (int n = 0; n < attrs.getLength(); n++) { Attr a = (Attr) attrs.item(n); String name;//from www . ja va 2 s. com if ((name = a.getName()).startsWith("xmlns:") && a.getNodeValue().equals(uri)) { return name.substring(6); } } e = e.getParentNode(); } return null; }
From source file:Main.java
/** * Checks if is element exists by tag name. * @param element the element/* w w w. ja v a 2 s .c o m*/ * @param tagName the tag name * @return true, if is element exists by tag name */ public static boolean isElementExistsByTagName(final Element element, final String tagName) { if (element == null || tagName == null || tagName.isEmpty()) { return false; } // Extract all children on element final NodeList res = element.getChildNodes(); for (int i = 0; i < res.getLength(); i++) { final Node node = res.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { final Element elem = (Element) node; // Check matching with tagname expected if (elem.getTagName().equals(tagName)) { return true; } } } return false; }
From source file:Main.java
private static List<Element> getChildElementsByName(Element parentElement, String childElementName) { List<Element> elementList = new ArrayList<Element>(); NodeList childNodes = parentElement.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node node = childNodes.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { Element childElement = (Element) node; if (childElement.getNodeName().equals(childElementName)) { elementList.add(childElement); }/* w w w.j a va 2 s . com*/ } } return elementList; }
From source file:Main.java
public static List<Element> getNamedChildElements(final Element parent, final String name) { List<Element> elements = new ArrayList<Element>(); if (parent != null) { Node child = parent.getFirstChild(); while (child != null) { if ((child.getNodeType() == Node.ELEMENT_NODE) && (child.getNodeName().equals(name))) { elements.add((Element) child); }//w w w . j a v a 2 s . c om child = child.getNextSibling(); } } return elements; }
From source file:Main.java
/** * Returns next sibling element node//w w w . j a va2 s .co m * @param element * @return */ protected static Element getNextSiblingElement(Element element) { try { Node tmpNode = null; tmpNode = element.getNextSibling(); while (tmpNode.getNodeType() != Node.ELEMENT_NODE) { //fix for structure that has more than two parents null if (tmpNode.getNextSibling() == null) tmpNode = tmpNode.getParentNode(); tmpNode = tmpNode.getNextSibling(); } return (Element) tmpNode; } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:Main.java
/** * Get the previous sibling element of the specified element, null if it has no previous other siblings. * /*w w w .j av a 2 s . co m*/ * @param element The element to search siblings for. * @return The previous sibling element, null if it has none. */ public static Element getPrevSibling(Element element) { if (element == null) return null; Node node = element.getPreviousSibling(); while (node != null) { if (node.getNodeType() == Node.ELEMENT_NODE) return (Element) node; node = node.getPreviousSibling(); } return null; }
From source file:Main.java
/** * @param child// ww w .j a v a 2s. c o m * @return the single child element or null * @throws Exception if the child is present multiple times */ public static Element getFirstChild(Element element, String child) throws Exception { NodeList nodes = element.getChildNodes(); Element ret = null; for (int i = 0; i < nodes.getLength(); i++) { Node childNode = nodes.item(i); if (childNode.getNodeName().equalsIgnoreCase(child) && childNode.getNodeType() == Node.ELEMENT_NODE) { ret = (Element) childNode; return ret; } } return null; }