List of usage examples for org.w3c.dom Element getChildNodes
public NodeList getChildNodes();
NodeList
that contains all children of this node. From source file:Main.java
/** Return first child with given name within given element. */ public static Element getFirst(Element parent, String name) { NodeList nl = parent.getChildNodes(); for (int i = 0, l = nl.getLength(); i < l; i++) { Node curr = nl.item(i);/*from w w w. j a va 2 s . c o m*/ if (curr.getNodeType() == Element.ELEMENT_NODE && curr.getNodeName().equals(name)) return (Element) curr; } return null; }
From source file:Main.java
/** * Recursively removes all text nodes containing whitespace only from a document element. Also * trims leading and trailing whitespace from text nodes. * /* w w w .j a v a 2s . c o m*/ * @param e The root document element. */ public static void removeWhitespaceNodes(Element e) { NodeList children = e.getChildNodes(); for (int i = children.getLength() - 1; i >= 0; i--) { Node child = children.item(i); if (child instanceof Text && ((Text) child).getData().trim().length() == 0) e.removeChild(child); else if (child instanceof Text) child.setTextContent(((Text) child).getData().trim()); else if (child instanceof Element) removeWhitespaceNodes((Element) child); } }
From source file:Main.java
public static void removeElement(Element parent, String tagName) { NodeList nl = parent.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node nd = nl.item(i);// w w w .j av a2 s. c om if (nd.getNodeName().equals(tagName)) { parent.removeChild(nd); } } }
From source file:Main.java
/** * Extracts the first CDATA child from the given {@code org.w3c.dom.Element}. * * @param elem the parent Element//from ww w. ja v a 2 s.c o m * @return the String value of the CDATA section, or {@code null} if none * found */ public static String getCdata(Element elem) { NodeList nodes = elem.getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); if (node.getNodeType() == Node.CDATA_SECTION_NODE) { CharacterData cdataNode = (CharacterData) node; return cdataNode.getData(); } } return null; }
From source file:Main.java
public static void fixSize(final Element element) { final int len = element.getChildNodes().getLength(); if (len > 0) { element.setAttribute("size", "" + len); } else {// ww w. j a v a2s. c om element.getParentNode().removeChild(element); } }
From source file:Main.java
public static Element getChildElement(Element elem, String name) { NodeList nodes = elem.getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(name)) { return (Element) node; }/*from w ww .j a v a 2s . c om*/ } return null; }
From source file:Main.java
public static Element getElementByTagName(Element n, String elementName) { int sz = n.getChildNodes().getLength(); ArrayList<Element> elements = new ArrayList<Element>(sz); for (int idx = 0; idx < sz; idx++) { Node node = n.getChildNodes().item(idx); if (node instanceof Element && node.getLocalName().equals(elementName)) elements.add((Element) node); }/*from www. j a v a2 s . c o m*/ if (elements.size() > 0) return elements.get(0); return null; }
From source file:Main.java
public static int findFirstChildIndex(Element elem, String tagName) { NodeList nl = elem.getChildNodes(); for (int i = 0; i < nl.getLength(); ++i) { if (nl.item(i).getNodeName().equals(tagName)) return i; }//from www . j a va 2 s . co m return -1; }
From source file:Main.java
public static String getElementText(Element element) { NodeList nl = element.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node c = nl.item(i);/* ww w . j av a 2 s . c o m*/ if (c instanceof Text) { return ((Text) c).getData().trim(); } } return null; }
From source file:Main.java
public static void removeContent(Element element) { NodeList nodeList = element.getChildNodes(); while (nodeList.getLength() > 0) { element.removeChild(nodeList.item(0)); }//from w w w. ja v a2 s . c o m }