List of usage examples for org.w3c.dom Element getFirstChild
public Node getFirstChild();
From source file:Main.java
/** * Get the text that is associated with this element. * //from w w w. j av a 2 s . c o m * @param element an Element object. * @return the text that is associated with this element. */ public static String getText(Element element) { String text = null; // Get first child element Node node = element.getFirstChild(); // NodeList nodeList = element.getChildNodes(); // int length = nodeList.getLength(); // Process while there are nodes and the text hasn't been found while ((node != null) && (text == null)) { // If a text node or cdata section is found, then get text if ((node.getNodeType() == Node.TEXT_NODE) || (node.getNodeType() == Node.CDATA_SECTION_NODE)) { text = ((CharacterData) node).getData(); } // Get next sibling node = node.getNextSibling(); } if (text != null) text = text.trim(); return text; }
From source file:Main.java
/** * Get the first child element with the given name. This differs from {@link * org.w3c.dom.Node#getFirstChild Node.getFirstChild} in that this only returns elements. * * @param parent the parent node for which to get the first child * @param name the name of the child element to get; may be null or "*" to indicate the * first child element of any name * @return the child, or null if none was found *///from ww w . ja v a 2 s.c o m public static Element getFirstChild(Element parent, String name) { final String filter = (name != null && !name.equals("*")) ? name : null; for (Node n = parent.getFirstChild(); n != null; n = n.getNextSibling()) { if (n.getNodeType() == Node.ELEMENT_NODE && (filter == null || n.getNodeName().equals(filter))) return (Element) n; } return null; }
From source file:DOMEdit.java
public static void newEmail(Document doc, String newname, String newemail) { Element root = doc.getDocumentElement(); Element person = (Element) root.getFirstChild(); while (person != null) { Element name = (Element) person.getFirstChild(); Text nametext = (Text) name.getFirstChild(); String oldname = nametext.getData(); if (oldname.equals(newname)) { Element email = (Element) name.getNextSibling(); Text emailtext = (Text) email.getFirstChild(); emailtext.setData(newemail); }/*from w ww . j a v a 2s . co m*/ person = (Element) person.getNextSibling(); } }
From source file:Main.java
/** Locate a sub-element tagged 'name', return its value. * * Will only go one level down, not search the whole tree. * * @param element Element where to start looking. May be null. * @param name Name of sub-element to locate. * @param default_value Default value if not found * * @return Returns string that was found or default_value. *///from w w w. j a v a 2 s.c o m final public static String getSubelementString(final Element element, final String name, final String default_value) { if (element == null) return default_value; Node n = element.getFirstChild(); n = findFirstElementNode(n, name); if (n != null) { Node text_node = n.getFirstChild(); if (text_node == null) return default_value; return text_node.getNodeValue(); } return default_value; }
From source file:DOMEdit.java
public static void dupAttributes(Document doc) { Element root = doc.getDocumentElement(); Element personOne = (Element) root.getFirstChild(); Attr deptAttr = personOne.getAttributeNode("dept"); personOne.removeAttributeNode(deptAttr); String deptString = deptAttr.getValue(); personOne.setAttribute("dept", deptString + " updated"); String mailString = personOne.getAttribute("mail"); personOne.setAttribute("mail", mailString + " updated"); String titleString = personOne.getAttribute("title"); //personOne.removeAttribute("title"); personOne.setAttribute("title", titleString + " updated"); }/*from w ww . jav a2 s.co m*/
From source file:Main.java
/** * Find an element, return as a string.// w w w .j a v a 2 s. c o m * @param e The element that searches. * @param find What we are searching for. * @return The value found, default value otherwise. */ public static String findElementAsString(final Element e, final String find) { final Element el = findElement(e, find); if (el == null) { return null; } for (Node child = el.getFirstChild(); child != null; child = child.getNextSibling()) { if (!(child instanceof Text)) { continue; } return child.getNodeValue(); } return null; }
From source file:Main.java
public static synchronized Element[] getChildElements(Element element) { if (element == null) { return null; }/* w w w. j ava 2 s .c o m*/ Vector childs = new Vector(); for (Node node = element.getFirstChild(); node != null; node = node.getNextSibling()) { if (node instanceof Element) { childs.add((Element) node); } } Element[] elmt = new Element[childs.size()]; childs.toArray(elmt); return elmt; }
From source file:Main.java
/** * Return the text (node value) of the first node under this, works best if normalized. *//*from www . jav a 2 s . c o m*/ public static String elementValue(Element element) { if (element == null) return null; // make sure we get all the text there... element.normalize(); Node textNode = element.getFirstChild(); if (textNode == null) return null; StringBuffer valueBuffer = new StringBuffer(); do { if (textNode.getNodeType() == Node.CDATA_SECTION_NODE || textNode.getNodeType() == Node.TEXT_NODE) { valueBuffer.append(textNode.getNodeValue()); } } while ((textNode = textNode.getNextSibling()) != null); return valueBuffer.toString(); }
From source file:Main.java
/** * Get the value of the specified element * * @param element//from w w w . j a v a 2 s. c om * @return */ public static String getElementValue(Element element) { NodeList nl = element.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { if (nl.item(i).getNodeType() == Node.TEXT_NODE) { return element.getFirstChild().getNodeValue(); } } return null; }
From source file:Main.java
public static String getChildElementValueByTagName(Element parentElement, String childTag) { if (childTag.equals(parentElement.getNodeName())) { return getNodeValue(parentElement); }//from w w w .ja v a 2 s . co m for (Node temp = parentElement.getFirstChild(); temp != null; temp = temp.getNextSibling()) { if (temp.getNodeType() == Node.ELEMENT_NODE && childTag.equals(temp.getNodeName())) { return getNodeValue(temp); } } return null; }