List of usage examples for org.w3c.dom Element getFirstChild
public Node getFirstChild();
From source file:Main.java
public static synchronized String formatXml(Element e, int indent) { indent++;//from www. j a v a 2s .c o m for (Node n = e.getFirstChild(); n != null; n = n.getNextSibling()) { appendIndent(e, n, indent); } return e.toString(); }
From source file:Main.java
public static String getElementValue(String elementName, Element element) throws Exception { String value = null;//from w ww . j a v a 2 s . c om NodeList childNodes = element.getElementsByTagName(elementName); Element cn = (Element) childNodes.item(0); value = cn.getFirstChild().getNodeValue().trim(); //value = childNodes.item(0).getNodeValue().trim(); if (value == null) { throw new Exception("No element found with given name:" + elementName); } return value; }
From source file:Main.java
public static void setValueByElementName(Document document, String name, String value) { if (document == null) { return;//w w w . ja v a 2 s. c o m } NodeList nodeList = document.getElementsByTagName(name); if (nodeList != null && nodeList.getLength() > 0) { for (int i = 0; i < nodeList.getLength(); i++) { Element element = (Element) nodeList.item(i); ((Text) element.getFirstChild()).setData(value); } } }
From source file:Main.java
public static void removeChildren(Element element) { while (true) { Node child = element.getFirstChild(); if (child == null) { return; }/*from w ww .j a va 2 s.co m*/ element.removeChild(child); } }
From source file:Utils.java
public static String getElementText(Element e) { StringBuffer buf = new StringBuffer(); for (Node n = e.getFirstChild(); n != null; n = n.getNextSibling()) { if (n.getNodeType() == Node.TEXT_NODE || n.getNodeType() == Node.CDATA_SECTION_NODE) { buf.append(n.getNodeValue()); }/*from w w w . ja va 2s . c o m*/ } return buf.toString(); }
From source file:Main.java
/** * Helper function for quickly retrieving a String value of a given * XML element./* w ww.ja va 2 s. com*/ * @param ele The document element from which to pull the String value. * @param tagName The name of the node. * @return The String value of the given node in the element passed in. */ public static String getTextValue(Element ele, String tagName) { String textVal = null; NodeList nl = ele.getElementsByTagName(tagName); if (nl != null && nl.getLength() > 0) { Element el = (Element) nl.item(0); if (el.getFirstChild() != null) { textVal = el.getFirstChild().getNodeValue(); } else { textVal = ""; } } return decode(textVal); }
From source file:Main.java
public static Element getFirstChildElementByLocalName(Element parentElem, String localName) { Node child = parentElem.getFirstChild(); while (child != null) { if ((child.getNodeType() == Node.ELEMENT_NODE) && getLocalName(child).equals(localName)) { return (Element) child; }// www . j a v a2s . com child = child.getNextSibling(); } return null; }
From source file:Main.java
/** * I take a xml element and the tag name, look for the tag and get * the text content /* www . j av a2 s . c o m*/ * i.e for <employee><name>John</name></employee> xml snippet if * the Element points to employee node and tagName is name I will return John * @param ele * @param tagName * @return */ public static String getTextValue(Element ele, String tagName) { String textVal = null; NodeList nl = ele.getElementsByTagName(tagName); if (nl != null && nl.getLength() > 0) { Element el = (Element) nl.item(0); textVal = el.getFirstChild().getNodeValue(); if (textVal != null) { textVal = textVal.trim(); } } return textVal; }
From source file:Main.java
public static Element element(Element element, String tagName) { for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) { if (child instanceof Element && tagName.equals(child.getNodeName())) { return (Element) child; }//w ww .j a v a2 s . co m } return null; }
From source file:Main.java
private static void makeNamelist(Document doc) { String names = null;/*www.j av a2 s . c o m*/ Element root = doc.getDocumentElement(); NodeList nameElements = root.getElementsByTagName("name"); for (int i = 0; i < nameElements.getLength(); i++) { Element name = (Element) nameElements.item(i); Text nametext = (Text) name.getFirstChild(); if (names == null) { names = nametext.getData(); } else { names += ", " + nametext.getData(); } } Element namelist = doc.createElement("names"); Text namelisttext = doc.createTextNode(names); namelist.appendChild(namelisttext); root.insertBefore(namelist, root.getFirstChild()); }