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 findContainerWithAttributeValueElseCreate(Document document, Element parent, String element, String attributeName, String attributeValue) { NodeList nl = parent.getElementsByTagName(element); Element e;/* w ww .j a v a2 s.com*/ for (int i = 0; i < nl.getLength(); i++) { e = (Element) nl.item(i); if (e.getAttribute(attributeName).equals(attributeValue)) { return e; } } e = document.createElement(element); parent.appendChild(e); e.setAttribute(attributeName, attributeValue); return e; }
From source file:Main.java
/** * Get one element by tag name.//from w ww .j av a 2 s. c o m * @return An Element object or null if no matching element. */ //// // METHOD: getOneElement //// public static Element getOneElement(Element inParentElement, String inTagName) { Element retElement = null; try { NodeList wkNodeList = inParentElement.getElementsByTagName(inTagName); if (wkNodeList.getLength() > 0) retElement = (Element) wkNodeList.item(0); } catch (Exception excp) { ; } return retElement; }
From source file:Main.java
/** * Returns the string value specified with the given tag name in the specified element. Assumes * that there is only one tag of the speicified name in the element. * //from w ww . ja v a 2 s. c o m * @param element * @param tagName * @return the string value specified with the given tag name in the specified element */ public static String getSingleTagValue(Element element, String tagName) { NodeList nodes = element.getElementsByTagName(tagName); if (!hasUniqueNode(nodes)) { return null; } Element e = (Element) nodes.item(0); if (e == null) { return null; } return e.getFirstChild().getNodeValue(); }
From source file:Main.java
/** * @param parent the parent XML element/*w w w. j a va 2 s . c o m*/ * @param childName the child node name * @return the child XML element with specified node name or <code>null</code> if does not exist */ public static Element getChildElement(final Element parent, final String childName) { Element child = null; if (parent != null) { NodeList children = parent.getElementsByTagName(childName); if (children.getLength() > 0) { child = (Element) children.item(0); } } return child; }
From source file:Main.java
/** * Traverses the DOM tree to lookup the XPath. * /*from ww w. ja v a2s.co m*/ * A sample XPath would look like "Parent > Child > Grandchild" for a DOM * tree as following: * * <pre> * <Parent> * <Child> * <Grandchild> * </Grandchild> * </Child> * </Parent> * </pre> * * @param e * The DOM element to look under * @param xPath * A path to lookup * @param separator * String separator used to break up level hierarchy with in the * path * @return Node(s) found at the given path */ public static NodeList lookup(Element e, String xPath, String separator) { String[] trail = { xPath }; if (separator != null) { trail = xPath.split(separator); } Element parent = e; for (int i = 0; i < trail.length - 1; i++) { parent = (Element) parent.getElementsByTagName(trail[i].trim()).item(0); } return parent.getElementsByTagName(trail[trail.length - 1].trim()); }
From source file:Main.java
/** * Finds the first element which name matchtes a given tag name * that is locacted anywhere below the given parent. * // w w w . j ava 2 s .c o m * @param parent the parent element below which to search the child * @param tagName the (tag) name of the desired child element * @return the child element if an element of that name existed, or null otherwise */ static public Element findFirstChildDeep(Element parent, String tagName) { // Child Element suchen if (parent == null) return null; NodeList nl = parent.getElementsByTagName(tagName); for (int i = 0; i < nl.getLength(); i++) { if (nl.item(i).getNodeType() == Node.ELEMENT_NODE) return (Element) nl.item(i); } return null; }
From source file:nl.igorski.lib.utils.data.XMLTool.java
/** * get the value of a specific tag in an Element * * @param element {Element}//w w w .j av a2 s .c o m * @param tagName {String} name of the tag * @return {String} the tags value */ public static String getTagValue(Element element, String tagName) { try { NodeList list = element.getElementsByTagName(tagName); Element theElement = (Element) list.item(0); NodeList textList = theElement.getChildNodes(); return textList.item(0).getNodeValue().trim(); } catch (Exception e) { } return ""; }
From source file:XMLUtils.java
/** * Returns the value of the given node./*ww w . j a v a 2s. c o m*/ * @param base the element from where to search. * @param name of the element to get. * @return the value of this element. */ public static String getStringValueElement(final Element base, final String name) { String value = null; // Get element NodeList list = base.getElementsByTagName(name); if (list.getLength() == 1) { Element element = (Element) list.item(0); Node node = element.getFirstChild(); if (node != null) { value = node.getNodeValue(); } } else if (list.getLength() > 1) { throw new IllegalStateException("Element '" + name + "' on '" + base + "' should be unique but there are '" + list.getLength() + "' elements"); } if (value != null) { value = value.trim(); } return value; }
From source file:Main.java
/** * Get the XML children element of an XML element, but only those of a * certain type//from w w w . j av a 2 s . c o m * * @param parent * The parent element to get the children from * @param elementTag * The tag of the elements to return * @return The list of children {@link Element} of the parent */ public static List<Element> getChildElements(Element parent, String elementTag) { /* get the state providers and find the corresponding one */ NodeList nodes = parent.getElementsByTagName(elementTag); List<Element> childElements = new ArrayList<>(); for (int i = 0; i < nodes.getLength(); i++) { Element node = (Element) nodes.item(i); if (node.getParentNode().equals(parent)) { childElements.add(node); } } return childElements; }
From source file:Main.java
public static String getText(Element rootElem, String[] path) { NodeList nodes = rootElem.getElementsByTagName(path[0]); if (nodes == null || nodes.getLength() < 1) { // failsafe if first item is @attribute identifier // then read attribute value from rootElement. boolean isAttrText = path[0].charAt(0) == '@'; if (!isAttrText) return null; Attr attr = rootElem.getAttributeNode(path[0].substring(1)); return (attr != null ? attr.getValue() : null); }//from w w w .j av a2 s . c om Element element = (Element) nodes.item(0); return getText(element, path, 1); }