List of usage examples for org.w3c.dom Element getFirstChild
public Node getFirstChild();
From source file:org.jenkinsci.plugins.os_ci.openstack.StackStatusTest.java
private static String getCharacterDataFromElement(Element e) { Node child = e.getFirstChild(); if (child instanceof CharacterData) { CharacterData cd = (CharacterData) child; return cd.getData(); }/* w w w . j av a2 s. c om*/ return ""; }
From source file:Main.java
public static List<Element> getElementChilds(Element elem) { List<Element> elemList = new ArrayList<Element>(); for (Node child = elem.getFirstChild(); child != null; child = child.getNextSibling()) { if (child instanceof Element) { elemList.add((Element) child); }//from ww w . jav a 2s . co m } return elemList; }
From source file:Main.java
public static String getElementValue(Element inElement) { if (null == inElement) return null; Node wkFirstChild = inElement.getFirstChild(); if (null == wkFirstChild) return ""; else//from w w w. j a va2s .co m return wkFirstChild.getNodeValue(); }
From source file:Main.java
public static Element removeElementContentWhitespace(Element root) { boolean foundElt = false; for (Node n1 = root.getFirstChild(); n1 != null; n1 = n1.getNextSibling()) { if (n1.getNodeType() != Node.ELEMENT_NODE) continue; foundElt = true;/* w ww . j a v a2 s . c o m*/ removeElementContentWhitespace(Element.class.cast(n1)); } if (foundElt) { Node n1 = root.getFirstChild(); while (n1 != null) { Node n2 = n1.getNextSibling(); if (n1.getNodeType() == Node.TEXT_NODE && isEmptyText(Text.class.cast(n1))) { root.removeChild(n1); } n1 = n2; } } return root; }
From source file:Main.java
/** * Helper function for quickly retrieving an boolean value of a given * XML element, with a default initialization value passed in a parameter. * @param ele The document element from which to pull the boolean value. * @param tagName The name of the node./* ww w . j a v a2 s. c o m*/ * @param defaultValue The default value of the node if it's value can't be processed. * @return The boolean value of the given node in the element passed in. */ public static boolean getBooleanValue(Element ele, String tagName, boolean defaultValue) { boolean boolVal = defaultValue; NodeList nl = ele.getElementsByTagName(tagName); if (nl != null && nl.getLength() > 0) { Element el = (Element) nl.item(0); if (el.getFirstChild().getNodeValue() != null) { boolVal = "true".equals(el.getFirstChild().getNodeValue()); } else { boolVal = defaultValue; } } return boolVal; }
From source file:Main.java
/** Get string value of an element. * @param element Element/*from w w w .j av a2 s.c om*/ * @return String of the node. Empty string if nothing found. */ public static String getString(final Element element) { final Node text = element.getFirstChild(); if (text == null) // <empty /> node return ""; if ((text.getNodeType() == Node.TEXT_NODE || text.getNodeType() == Node.CDATA_SECTION_NODE)) return text.getNodeValue(); return ""; }
From source file:Main.java
public static void insertBeforeFirstChild(Element object, Element attrId) { if (object.hasChildNodes()) { object.insertBefore(attrId, object.getFirstChild()); } else {//from ww w . j a v a2s.c o m object.appendChild(attrId); } }
From source file:Main.java
static public void setElementText(Element elm, String text) { Node node = elm.getFirstChild(); if (node == null) { if (text != null) elm.appendChild(elm.getOwnerDocument().createTextNode(text)); } else if (node.getNodeType() == Node.TEXT_NODE) { if (text == null) node.getParentNode().removeChild(node); else// w ww . ja v a 2 s. co m node.setNodeValue(text); } else if (text != null) { Text textNode = node.getOwnerDocument().createTextNode(text); elm.insertBefore(textNode, elm.getFirstChild()); } }
From source file:Utils.java
/** * Return the first element child with the specified qualified name. * /* w ww .j a va 2s .com*/ * @param parent * @param ns * @param lp * @return */ public static Element getFirstChildWithName(Element parent, String ns, String lp) { for (Node n = parent.getFirstChild(); n != null; n = n.getNextSibling()) { if (n instanceof Element) { Element e = (Element) n; String ens = (e.getNamespaceURI() == null) ? "" : e.getNamespaceURI(); if (ns.equals(ens) && lp.equals(e.getLocalName())) { return e; } } } return null; }
From source file:Main.java
public static void getAllAttrs(Element parent, String name, List<String> lst) { for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) { if (child.getNodeType() == Node.ELEMENT_NODE) { getAllAttrValueByName(child, name); List<String> tmp = getAllAttrValueByName(child, name); if (tmp != null) { lst.addAll(tmp);//w w w . j a va 2 s.c o m } else { // recursive childnodes getAllAttrs((Element) child, name, lst); } } } }