List of usage examples for org.w3c.dom Element getFirstChild
public Node getFirstChild();
From source file:Main.java
public static List<Element> elements(Element element, String tagName) { if (element == null || !element.hasChildNodes()) { return Collections.emptyList(); }//from w w w . jav a 2s .co m List<Element> elements = new ArrayList<Element>(); for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) { if (child.getNodeType() == Node.ELEMENT_NODE) { Element childElement = (Element) child; String childTagName = childElement.getLocalName(); if (tagName.equals(childTagName)) elements.add(childElement); } } return elements; }
From source file:DOMUtils.java
/** * Count number of children of a certain type of the given element. * * @param elem the element whose kids are to be counted * * @return the number of matching kids.//w w w . j av a 2 s . c o m */ public static int countKids(Element elem, short nodeType) { int nkids = 0; for (Node n = elem.getFirstChild(); n != null; n = n.getNextSibling()) { if (n.getNodeType() == nodeType) { nkids++; } } return nkids; }
From source file:Main.java
public static List<Element> elements(Element element, Set<String> allowedTagNames) { if (element == null || !element.hasChildNodes()) { return Collections.emptyList(); }/*from w w w.j a v a 2 s .c o m*/ List<Element> elements = new ArrayList<Element>(); for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) { if (child.getNodeType() == Node.ELEMENT_NODE) { Element childElement = (Element) child; if (allowedTagNames.contains(child.getLocalName())) { elements.add(childElement); } } } return elements; }
From source file:Main.java
public static List<Element> elements(Element element, String namespace, String localName) { if (element == null || !element.hasChildNodes()) { return Collections.emptyList(); }/*from www. j a va 2s .c om*/ List<Element> elements = new ArrayList<Element>(); for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) { String childNamespace = element.getNamespaceURI(); if (child.getNodeType() == Node.ELEMENT_NODE && (namespace != null ? namespace.equals(childNamespace) : childNamespace == null) && localName.equals(element.getLocalName())) { elements.add((Element) child); } } return elements; }
From source file:DOMUtils.java
/** * Concat all the text and cdata node children of this elem and return * the resulting text.//from www .java 2 s . c om * * @param parentEl the element whose cdata/text node values are to * be combined. * @return the concatanated string. */ static public String getChildCharacterData(Element parentEl) { if (parentEl == null) { return null; } Node tempNode = parentEl.getFirstChild(); StringBuffer strBuf = new StringBuffer(); CharacterData charData; while (tempNode != null) { switch (tempNode.getNodeType()) { case Node.TEXT_NODE: case Node.CDATA_SECTION_NODE: charData = (CharacterData) tempNode; strBuf.append(charData.getData()); break; } tempNode = tempNode.getNextSibling(); } return strBuf.toString(); }
From source file:importer.handler.post.stages.Discriminator.java
/** * Get the next child that is an element * @param elem the element/*from www . j a v a 2s .c o m*/ * @return its first child of elem or null */ static Element firstChild(Element elem) { Node n = elem.getFirstChild(); while (n != null && n.getNodeType() != Node.ELEMENT_NODE) n = n.getNextSibling(); return (Element) n; }
From source file:Main.java
/** * Set the text content of an element. All exisitng Text Node are * removed before adding a new Text Node containing the given text. * * @param element target element to set text content, cannot be null. * @param text content of the element, cannot be null. *//*from ww w . j a v a 2 s . c o m*/ public static void setElementText(Element element, String text) { // Remove all text element NodeList list = element.getChildNodes(); int len = list.getLength(); for (int i = 0; i < len; i++) { Node n = list.item(i); if (n.getNodeType() == Node.TEXT_NODE) { element.removeChild(n); } } Node child = element.getFirstChild(); Node textnode = element.getOwnerDocument().createTextNode(text); // insert text node as first child if (child == null) { element.appendChild(textnode); } else { element.insertBefore(textnode, child); } }
From source file:Main.java
public static void edit(Document doc) { Element element = doc.getDocumentElement(); Element element2 = doc.createElement("newname"); NamedNodeMap attrs = element.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { Attr attr2 = (Attr) doc.importNode(attrs.item(i), true); element2.getAttributes().setNamedItem(attr2); }// w ww . j a v a 2 s. c o m while (element.hasChildNodes()) { element2.appendChild(element.getFirstChild()); } element.getParentNode().replaceChild(element2, element); }
From source file:com.ibm.bi.dml.conf.DMLConfig.java
/** * Method to update the string value of an element identified by tagname * @param ele/*from w w w. ja v a 2 s . c om*/ * @param tagName * @param newTextValue */ private static void setTextValue(Element element, String tagName, String newTextValue) { NodeList list = element.getElementsByTagName(tagName); if (list != null && list.getLength() > 0) { Element elem = (Element) list.item(0); elem.getFirstChild().setNodeValue(newTextValue); } }
From source file:com.ibm.bi.dml.conf.DMLConfig.java
/** * Method to get the string value of an element identified by tag * @param ele/*from w w w . j a v a 2s . c o m*/ * @param tagName * @return */ private static String getTextValue(Element element, String tagName) { String textVal = null; NodeList list = element.getElementsByTagName(tagName); if (list != null && list.getLength() > 0) { Element elem = (Element) list.item(0); textVal = elem.getFirstChild().getNodeValue(); } return textVal; }