List of usage examples for org.w3c.dom Node getNodeType
public short getNodeType();
From source file:Main.java
public static List<Element> getChildElements(Element element) { List<Element> elements = new LinkedList<Element>(); NodeList children = element.getChildNodes(); if (children == null) { return elements; }/* www . j ava 2 s .c om*/ for (int i = 0, j = children.getLength(); i < j; i++) { Node child = children.item(i); if (child.getNodeType() == Node.ELEMENT_NODE) { elements.add((Element) child); } } return elements; }
From source file:Main.java
static public Element selectSingleElement(Element element, String xpathExpression) throws Exception { 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)) { return (Element) node; }/* www . j a v a 2 s . c om*/ } // NodeList nodes = element.getElementsByTagName(xpathExpression); // if (nodes.getLength() > 0) { // return (Element) nodes.item(0); // } else { return null; // } } else { XPath xpath = XPathFactory.newInstance().newXPath(); Element node = (Element) xpath.evaluate(xpathExpression, element, XPathConstants.NODE); return node; } }
From source file:Main.java
public static List getChildren(Element parentEl, String name) { List children = new ArrayList(); NodeList l = parentEl.getChildNodes(); for (int i = 0; i < l.getLength(); i++) { Node n = l.item(i); if (Node.ELEMENT_NODE == n.getNodeType() && name.equals(n.getNodeName())) { children.add((Element) n); }/*from ww w . j av a2 s . com*/ } return children; }
From source file:Main.java
public static Element getChild(Element parent, String name) { NodeList children = parent.getChildNodes(); final int length = children.getLength(); for (int index = 0; index < length; index++) { Node node = children.item(index); if (node.getNodeType() != Node.ELEMENT_NODE) { continue; }/*from ww w.ja v a 2 s .c om*/ Element element = (Element) node; if (name.equalsIgnoreCase(element.getNodeName())) { return element; } } return null; }
From source file:Main.java
public static String getTextContent(Element elm) { if (elm == null) { return null; }//w w w. ja va 2s . co m StringBuilder result = new StringBuilder(); NodeList childNodes = elm.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node node = childNodes.item(i); if ((node == null) || (node.getNodeType() != Node.TEXT_NODE)) { continue; } result.append(((Text) node).getData()); } return result.toString().trim(); }
From source file:Main.java
public static Element getNextElement(Element el) { Node node = el.getNextSibling(); while (node != null) { if (node.getNodeType() == Node.ELEMENT_NODE) return (Element) node; node = node.getNextSibling();/*from w w w . j ava 2 s . c o m*/ } return null; }
From source file:Main.java
/** * Returns true if element node has children elements * @param actElement/* w w w .j a va2 s. com*/ * @return */ protected static boolean hasChildElementNodes(Element actElement) { try { if (actElement.hasChildNodes()) { NodeList childNodes = actElement.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node element = childNodes.item(i); if (element.getNodeType() == Node.ELEMENT_NODE) { return true; } } } return false; } catch (Exception e) { e.printStackTrace(); return false; } }
From source file:Main.java
public static Node cloneNode(Document d, Node n) { Node r = null;//from w w w . ja va 2 s . c o m switch (n.getNodeType()) { case Node.TEXT_NODE: r = d.createTextNode(((Text) n).getData()); break; case Node.CDATA_SECTION_NODE: r = d.createCDATASection(((CDATASection) n).getData()); break; case Node.ELEMENT_NODE: r = d.createElement(((Element) n).getTagName()); NamedNodeMap map = n.getAttributes(); for (int i = 0; i < map.getLength(); i++) { ((Element) r).setAttribute(((Attr) map.item(i)).getName(), ((Attr) map.item(i)).getValue()); } break; } return r; }
From source file:Main.java
/** Returns all the direct child elements of the given element. */ public static List<Element> getElements(Element element) { List<Element> elements = new ArrayList<>(); for (Node node : toIterable(element.getChildNodes())) { if (node.getNodeType() == Node.ELEMENT_NODE) { elements.add((Element) node); }//from w w w. j ava2s . c o m } return elements; }
From source file:Main.java
public static Element getRootElement(Document doc) { Node node = doc.getFirstChild(); while (node != null) { if (node.getNodeType() == Node.ELEMENT_NODE) return (Element) node; node = node.getNextSibling();/*from w w w .j av a 2 s.co m*/ } return null; }