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
public static String getElementText(Element n) { NodeList childNodes = n.getChildNodes(); String result = new String(); for (int i = 0; i < childNodes.getLength(); i++) { Node node = childNodes.item(i); if (node.getNodeType() == Node.TEXT_NODE) { result += node.getNodeValue(); }//from ww w . j ava 2 s . co m } return result; }
From source file:Main.java
public static Object getContent(Element element) { NodeList nl = element.getChildNodes(); StringBuffer content = new StringBuffer(); for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i);/*from w ww. j a v a 2 s .co m*/ switch (node.getNodeType()) { case Node.ELEMENT_NODE: return node; case Node.CDATA_SECTION_NODE: case Node.TEXT_NODE: content.append(node.getNodeValue()); break; } } return content.toString().trim(); }
From source file:Main.java
public static List<Element> getChildren(Element parent) { NodeList children = parent.getChildNodes(); final int length = children.getLength(); ArrayList<Element> elements = new ArrayList<Element>(); for (int index = 0; index < length; index++) { Node node = children.item(index); if (node.getNodeType() != Node.ELEMENT_NODE) { continue; }//from w ww. jav a 2s . c o m elements.add((Element) node); } return elements; }
From source file:Main.java
public static Element getChildElementByTagName(Element ele, String childEleName) { NodeList nl = ele.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i);/*from w w w. j av a2s .c o m*/ if (node instanceof Element && childEleName.equals(node.getNodeName()) || childEleName.equals(node.getLocalName())) { return (Element) node; } } return null; }
From source file:Main.java
public final static Element subElement(Element superEle, String subName) { NodeList list = superEle.getChildNodes(); if (list == null) { return null; }/*from ww w. j a v a2s. c o m*/ for (int i = 0; i < list.getLength(); i++) { Node node = list.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { if (node.getNodeName().equals(subName)) { return (Element) node; } } } return null; }
From source file:Main.java
private static void removeWhitespaces(Element element) { NodeList children = element.getChildNodes(); for (int i = 0; i < children.getLength();) { Node n = children.item(i); if (n instanceof Element) { removeWhitespaces((Element) n); ++i;/* w ww .j a v a2s . c om*/ } else if (n instanceof Text && ((Text) n).getTextContent().trim().length() == 0) { element.removeChild(n); } else { ++i; } } }
From source file:Main.java
/** * Traverses through a DOM tree starting at the given element and removes all those text-nodes from it that only * contain white spaces.// w ww . j av a 2s .c om * * @param parent * the parent Element */ public static void removeWhitespaceTextNodes(Element parent) { final NodeList nl = parent.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { final Node child = nl.item(i); if (child.getNodeType() == Node.TEXT_NODE) { if (child.getNodeValue().trim().length() == 0) { parent.removeChild(child); i--; // since the child is removed counting up must be made undone } } else if (child.getNodeType() == Node.ELEMENT_NODE && child.getChildNodes().getLength() > 0) { removeWhitespaceTextNodes((Element) child); } } }
From source file:Main.java
/** * Get the Element children of an element as an array. * @param element// w w w . j a va2 s .c o m * @return non-<code>null</code> array */ public static Element[] getChildElements(Element element) { NodeList childNodes = element.getChildNodes(); List<Element> kids = new ArrayList<Element>(); Node node = childNodes.item(0); while (node != null) { if (node instanceof Element) { kids.add((Element) node); } node = node.getNextSibling(); } return (Element[]) kids.toArray(new Element[kids.size()]); }
From source file:Main.java
public static Element getChildWithAttribute(Element parent, String attrName) { NodeList childNodes = parent.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node node = childNodes.item(i); if (node instanceof Element) { Element element = (Element) node; if (!"".equals(element.getAttribute(attrName))) { return element; }/*from w ww . jav a2s. c om*/ } } return null; }
From source file:Main.java
License:asdf
public static void newEmail(Document doc, String newname, String newemail) { Element root = doc.getDocumentElement(); NodeList rootlist = root.getChildNodes(); for (int i = 0; i < rootlist.getLength(); i++) { Element person = (Element) rootlist.item(i); NodeList personlist = person.getChildNodes(); Element name = (Element) personlist.item(0); NodeList namelist = name.getChildNodes(); Text nametext = (Text) namelist.item(0); String oldname = nametext.getData(); if (oldname.equals(newname)) { Element email = (Element) personlist.item(1); NodeList emaillist = email.getChildNodes(); Text emailtext = (Text) emaillist.item(0); emailtext.setData(newemail); }/* w w w . j a v a2s .c o m*/ } }