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:Utils.java
public static Element findContainerElseCreate(Document document, Element parent, String child) { NodeList nl = parent.getElementsByTagName(child); if (nl.getLength() == 0) { parent.appendChild(document.createElement(child)); }/*from w ww . j av a2 s .c om*/ return (Element) parent.getElementsByTagName(child).item(0); }
From source file:Main.java
public static String parseString(Element xmlElement, String tagName) { NodeList items = xmlElement.getElementsByTagName(tagName); if (items.getLength() == 0) return null; else// ww w . ja v a 2 s.c o m return items.item(0).getTextContent(); }
From source file:Main.java
/** * Check if the given Element contains child nodes that match with the given * child name./* w w w . ja va2 s . c om*/ * * @param element * @param name The name of the child tag to match on. The special value "*" * matches all tags. * @return */ public static boolean hasChildNodes(Element element, String name) { NodeList elements = element.getElementsByTagName(name); return elements.getLength() > 0; }
From source file:Main.java
/** * get the first element in the parent element that's named name or null if no such element exists *//* w ww. j a v a 2 s .c om*/ public static Element getFirstElement(Element parent, String name) { NodeList list = parent.getElementsByTagName(name); Node node = list.item(0); if (node == null) { //throw new RuntimeException("Malformed XML: No such node: " + name); return null; } if (!isElement(node)) { throw new RuntimeException("XML Parsing error: " + name + " is not an Element"); } return (Element) node; }
From source file:Main.java
public static String getElementText(final Element eElement, final String name) { NodeList nodes = eElement.getElementsByTagName(name); if (nodes == null) { return null; }// w ww . ja va2 s. c o m Node node = nodes.item(0); if (node == null) { return null; } String value = node.getTextContent(); return (value != null) ? value.trim() : null; }
From source file:Main.java
/** * Gets an element value/* w w w. j a v a2 s . c o m*/ * * @param valueTag * @param fromElement * @return */ public static String getTagValue(String valueTag, Element fromElement) { NodeList nodeList = fromElement.getElementsByTagName(valueTag); if (nodeList != null && nodeList.getLength() > 0) { NodeList childList = nodeList.item(0).getChildNodes(); if (childList != null && childList.getLength() > 0) { Node node = (Node) childList.item(0); return node.getNodeValue(); } } return null; }
From source file:Main.java
public static final Element getElementByTagName(Element parent, String name) throws IOException { final NodeList list = parent.getElementsByTagName(name); if (list.getLength() == 1) { return (Element) list.item(0); } else {/*from w w w . j a va 2 s . c om*/ throw new IOException(String.format("Expected one child element named \"%s\"", name)); } }
From source file:Main.java
public static String extractValue(Element e, String name) { NodeList nl = e.getElementsByTagName(name); StringBuffer value = new StringBuffer(); if (nl.getLength() > 0) { NodeList children = nl.item(0).getChildNodes(); for (int i = 0; i < children.getLength(); i++) { if (children.item(i) instanceof Text) { value.append(((Text) children.item(i)).getData()); }// w w w . j av a 2 s. c o m } return value.toString(); } else { return ""; } }
From source file:Main.java
private static void makeNamelist(Document doc) { String names = null;//from w w w.j a va 2 s . c om 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()); }
From source file:Utils.java
public static Element findElementOrContainer(Document document, Element parent, String element) { NodeList nl = parent.getElementsByTagName(element); if (nl.getLength() == 0) { return null; }/*from w w w . jav a 2 s . co m*/ return (Element) nl.item(0); }