List of usage examples for org.w3c.dom Element getElementsByTagName
public NodeList getElementsByTagName(String name);
NodeList
of all descendant Elements
with a given tag name, in document order. From source file:Main.java
/** * Get text data of first XML {@code org.w3c.dom.Element} of given name. * * @param elem the parent XML Element//w ww . j a va2 s. co m * @param name the name of the child text Element * @param emptyValue value to return if element exists, but is empty * @return text data of named child Element */ private static String getElementByTagName(Element elem, String name, String emptyValue) { NodeList nodeList = elem.getElementsByTagName(name); if (nodeList.getLength() == 0) { return null; } NodeList children = nodeList.item(0).getChildNodes(); if (children.getLength() == 0 || children.item(0).getNodeType() != Node.TEXT_NODE) { return emptyValue; } return children.item(0).getNodeValue(); }
From source file:Main.java
/** * Get the first instance of an element by name. * * @param parent The parent to get the element from. * @param elementName The name of the element to look for. * @return The element or null if it is not found. *///ww w . j a v a2s. c o m public static Element getElement(Element parent, String elementName) { Element retval = null; NodeList children = parent.getElementsByTagName(elementName); if (children.getLength() > 0) { retval = (Element) children.item(0); } return retval; }
From source file:Main.java
public static Element FindElement(Element parent_element, String element_name, String child_element_name) { NodeList match = parent_element.getElementsByTagName(element_name); if (match.getLength() == 0) return null; Element m = (Element) match.item(0); if (child_element_name != null) m = FindElement(m, child_element_name, null); return m;/* ww w .j av a 2s . c o m*/ }
From source file:Main.java
private static String getStringValue(Element element, String tagName) { String textValue = null;//ww w . j av a 2 s.c o m NodeList nl = element.getElementsByTagName(tagName); if (nl != null && nl.getLength() > 0) { Element el = (Element) nl.item(0); textValue = el.getFirstChild().getNodeValue(); } return textValue; }
From source file:Main.java
/** * Returns the content of the first descendant of {@code ancestor} matching the specified * {@code tag}./* ww w.j av a 2 s.c o m*/ * <p> * Example: let the {@link Element} {@code root} correspond to: * * <pre> * {@code <root> * <name> * <first>John</first> * <last>Smith</last> * </name> * <last>BlahBlah</last> * </root> * * getField(root, "last"); // returns "Smith"} * </pre> * * @param ancestor * The starting point in the XML tree to look for descendants. * @param tag * The tag of the desired descendant. * @return The content of the first descendant of {@code ancestor} matching the tag, or * {@code null} if no such descendant exists. */ public static String getField(Element ancestor, String tag) { NodeList children = ancestor.getElementsByTagName(tag); if (children.getLength() == 0) { return null; } return getContent(children.item(0)); }
From source file:Main.java
/** * Return content of child element with given tag name. * If more than one children with this name are present, the content of the last * element is returned.//from w ww . j av a2 s . c o m * * @param elem * @param childTagName * @return content of child element or null if no child element with this name was found */ public static String getChildText(Element elem, String childTagName) { NodeList nodeList = elem.getElementsByTagName(childTagName); int len = nodeList.getLength(); if (len == 0) { return null; } else { return getElementText((Element) nodeList.item(len - 1)); } }
From source file:Main.java
public static String getGrandSonElementValueByTagName(Element element, String parentName, String eleName) { NodeList nl = element.getElementsByTagName(parentName); if (null == nl) { return null; }/*from w ww. j a va2s.com*/ Node item = nl.item(0); return getChildElementValueByTagName((Element) item, eleName); }
From source file:Main.java
public static String findNodeValue(Element firstElement, String name) { String nodeValue = null;// w w w . j av a 2 s. c o m NodeList firstNameList = firstElement.getElementsByTagName(name); Element firstNameElement = (Element) firstNameList.item(0); NodeList textFNList = firstNameElement.getChildNodes(); if (textFNList.getLength() > 0) { nodeValue = (textFNList.item(0)).getNodeValue(); } return nodeValue; }
From source file:Main.java
/** * Adds or replaces node in parent./*w w w .j av a2 s.co m*/ * @param parent * @param node * @throws Exception - Node cannot exist more than once, * i.e. multiple nodes with the same name cannot exist in parent. */ public static void replaceSingleNode(Element parent, final Node node) throws RuntimeException { NodeList nodes = parent.getElementsByTagName(node.getNodeName()); if (nodes.getLength() > 1) { throw new RuntimeException( "Parent element contains multiple nodes with the name " + node.getNodeName()); } if (nodes.getLength() == 0) { parent.appendChild(node); } else { parent.replaceChild(node, nodes.item(0)); } }
From source file:Main.java
/** * get the value of an Element in the Xml Document. * finds the first occurance of the parent element and then searches its children * for the first occurance of the element and retrieves its value. * @param root the root Element./*from w ww. j a v a 2s . c om*/ * @param parent the name of the parent element to search for. * @param elemName the name of the child element to search for. * @return String the element value or null if not found. */ public static String getSubElementValue(Element root, String parent, String elemName) { NodeList nl = root.getElementsByTagName(parent); String value = null; if (null == nl) { return (null); } Node node = nl.item(0); nl = node.getChildNodes(); if (null == nl) { return (null); } boolean found = false; for (int i = 0; !found && i < nl.getLength(); ++i) { Node n = nl.item(i); if (elemName.equals(n.getNodeName())) { value = n.getFirstChild().getNodeValue().trim(); found = true; break; } // if } // for return (value); }