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
public static Vector getElementsByTagAndAttribValue(org.w3c.dom.Element element, String tag, String attrib, String val) { NodeList desElements = element.getElementsByTagName(tag); Vector selElements = new Vector(desElements.getLength() / 10, 10); for (int i = 0; i < desElements.getLength(); i++) { org.w3c.dom.Node desElement = desElements.item(i); if (desElement.getNodeType() == org.w3c.dom.Element.ELEMENT_NODE) { NamedNodeMap attributeNodes = desElement.getAttributes(); org.w3c.dom.Node selAttribNode = attributeNodes.getNamedItem(attrib); if (selAttribNode != null && selAttribNode.getNodeValue().equalsIgnoreCase(val)) { selElements.add(desElement); }/*from w w w. j a v a2 s.c om*/ } } return selElements; }
From source file:Main.java
public static String getAttributeFromElement(Element element, String name, String attributeName) { String result = null;/* w ww. j a va 2 s .c om*/ Element nameElement = (Element) element.getElementsByTagName(name).item(0); if (nameElement != null) { result = nameElement.getAttribute(attributeName).toString(); } if (result != null && result.length() == 0) { result = null; } return result; }
From source file:Main.java
private static void stripNamedElements(Document document, String name) { Element root = document.getDocumentElement(); if (root != null) { NodeList nodes = root.getElementsByTagName(name); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); node.getParentNode().removeChild(node); }/*from w w w . ja va 2s . c o m*/ } }
From source file:Main.java
public static List<String> parseChildNodesAsList(Element current, String name) { List<String> result = new ArrayList<String>(); NodeList nodes = current.getElementsByTagName(name); for (int i = 0; i < nodes.getLength(); ++i) { Element e = (Element) nodes.item(i); Node child = e.getFirstChild(); if (child == null) continue; result.add(child.getNodeValue()); }//from www .j a v a 2 s .c om return result; }
From source file:Main.java
/** * Retrieves the DOM <code>NodeList</code> contained by the named element * in the given parent element.<br> * <br>//from ww w . j av a 2 s . c o m * <pre> * <parent-element> * <named-list> * <list-item/> * <list-item/> * </named-list> * </parent-element> * </pre> * * In the above example, a <code>NodeList</code> containing the two * <pre><list-item/></pre> elements would be returned by passing the parent * element and "named-list" into the function parameters. * * @param parent The parent DOM element that contains the named list. * @param containerTagName The name of the element that contains the list items. * @return A <code>NodeList</code> containing the list items. * @throws Exception If an exception occurs while traversing the DOM objects. */ public static NodeList getNamedNodeList(Element parent, String containerTagName) throws Exception { NodeList nl = parent.getElementsByTagName(containerTagName); if (nl.getLength() < 1) { throw new Exception("Missing named list: " + containerTagName); } return nl.item(0).getChildNodes(); }
From source file:Main.java
/** * Get the attribute values of a given name/value pair for * the first XML {@code org.w3c.dom.Element} of given name. * * @param elem the parent XML Element/*from w ww .j a v a2 s .c o m*/ * @param name the name of the child text Element * @return attribute name and value Map of named child Element */ public static Map<String, String> getAllAttributes(Element elem, String name) { Map<String, String> attributes = new TreeMap<String, String>(); NodeList nodeList = elem.getElementsByTagName(name); int length = nodeList.getLength(); for (int n = 0; n < length; ++n) { attributes.put(((Element) nodeList.item(n)).getAttribute("name"), ((Element) nodeList.item(n)).getAttribute("value")); } return attributes; }
From source file:Main.java
/** Returns the first 'subElement' of 'node'. */ public static Element findFirst(Element node, String subElement) { Element ans = null;/*www.j a v a2 s . c o m*/ if (node != null) { NodeList nl = node.getElementsByTagName(subElement); if (nl != null && nl.getLength() > 0) ans = (Element) nl.item(0); } return ans; }
From source file:Main.java
public static String[] getChildrenAsStrings(Element parent, String childName) { if (doesElementContainChildren(parent, childName)) { NodeList children = parent.getElementsByTagName(childName); String[] strings = new String[children.getLength()]; for (int i = 0; i < children.getLength(); i++) { strings[i] = children.item(i).getTextContent(); }/*from w w w. j av a 2s. com*/ return strings; } return null; }
From source file:Main.java
/** * Gets the value from the first descendant of a given ancestor, where the * descendant has the specified name./*from w w w . ja va2 s . co m*/ * * @param ancestor * the ancestor element * @param tagname * the tag name of the descendant to find. * @return the value of the descendant or <code>null</code> if no descendant * with that name could be found or the descendant found has no * value. */ public static String getValueFromDescendant(Element ancestor, String tagname) { if (ancestor != null) { NodeList nl = ancestor.getElementsByTagName(tagname); if (!isEmpty(nl)) { Element e = (Element) nl.item(0); Node c = e.getFirstChild(); if (c != null) { return c.getNodeValue(); } } } return null; }
From source file:Main.java
/** * Gets the child element.// ww w . j a va2 s. c o m * * @param el * @param tagName * * @return the child element */ public static Element getChildElement(Element el, String tagName) { if (el == null) { return null; } NodeList list = el.getElementsByTagName(tagName); if (list != null && list.getLength() > 0) { return (Element) list.item(0); } return null; }