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
/** * Update a property of a given configuration file and return a string representation of the * xml document//from w w w. j a va 2 s.c om * @param doc * @param property * @param newValue * @throws IllegalAccessException * @throws InstantiationException * @throws ClassNotFoundException */ public static String updateXmlDoc(Document doc, List<String> properties) throws ClassNotFoundException, InstantiationException, IllegalAccessException { for (String property : properties) { String key = property.split(":")[0]; String value = property.split(":")[1]; NodeList propertiesNode = doc.getElementsByTagName("esf:property"); for (int i = 0; i < propertiesNode.getLength(); i++) { Node node = propertiesNode.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { Element el = (Element) node; if (key.equals(el.getAttribute("name"))) { el.getElementsByTagName("esf:value").item(0).setTextContent(value); } } } } return parseXmlDocToString(doc); }
From source file:Main.java
public static boolean updateTextNode(String nodeName, Element searchFrom, String data, int position) throws Exception { boolean result = false; Element currentElement = (Element) searchFrom.getElementsByTagName(nodeName).item(position); if (currentElement != null && currentElement.getNodeType() == Node.ELEMENT_NODE) { Text textNode = (Text) (currentElement.getFirstChild()); textNode.setData(data);//from w w w . j a va 2 s .co m if (textNode != null && textNode.getNodeValue() == null) result = false; else result = true; } return result; }
From source file:Main.java
public static String getNamedElemXml(Element parent, String elementName) throws Exception { String val = null; NodeList list = parent.getElementsByTagName(elementName); if (list.getLength() > 0) { val = getXmlString(list.item(0)); }// w w w. j a v a2 s .co m return val; }
From source file:Main.java
/** * Given a todo element, we must get the element specified * by the tagName, then must traverse that Node to get the * value.// www.j a v a 2 s. c om * @param e * @param tagName * @return the value of the corresponding element */ public static String getValue(Element e, String tagName) { try { // Get node lists of a tag name from an Element NodeList elements = e.getElementsByTagName(tagName); Node node = elements.item(0); NodeList nodes = node.getChildNodes(); // Find a value whose value is non-whitespace String s; for (int i = 0; i < nodes.getLength(); i++) { s = ((Node) nodes.item(i)).getNodeValue().trim(); if (s.equals("") || s.equals("\r")) { continue; } else { return s; } } } catch (Exception ex) { return null; } return null; }
From source file:Main.java
/** * Gets the unique child node of the given <code>parent</code> element.<br> * @param parent the parent node/*from w w w . j av a 2 s .c o m*/ * @param childName name of the wanted child node * @return the unique child of the parent node */ public static Element getUniqueChild(Element parent, String childName) { Element child = null; if (parent != null && childName != null) { NodeList list = parent.getElementsByTagName(childName); if (list != null && list.getLength() > 0) { child = (Element) list.item(0); } } return child; }
From source file:Main.java
public static List<Element> getElements(NodeList nodes, String[] path) { if (nodes == null) return null; for (int i = 1; i < path.length; i++) { Element element = (Element) nodes.item(0); if (element == null) break; nodes = element.getElementsByTagName(path[i]); }//from w w w . j a v a 2s . co m if ((nodes == null) || (nodes.getLength() < 1)) return null; List<Element> list = new ArrayList<Element>(); for (int i = 0; i < nodes.getLength(); i++) { Element element = (Element) nodes.item(i); list.add(element); } return (list.size() < 1 ? null : list); }
From source file:Main.java
/** * Helper function for quickly retrieving an boolean value of a given * XML element, with a default initialization value passed in a parameter. * @param ele The document element from which to pull the boolean value. * @param tagName The name of the node.//from www. ja v a 2s. c o m * @param defaultValue The default value of the node if it's value can't be processed. * @return The boolean value of the given node in the element passed in. */ public static boolean getBooleanValue(Element ele, String tagName, boolean defaultValue) { boolean boolVal = defaultValue; NodeList nl = ele.getElementsByTagName(tagName); if (nl != null && nl.getLength() > 0) { Element el = (Element) nl.item(0); if (el.getFirstChild().getNodeValue() != null) { boolVal = "true".equals(el.getFirstChild().getNodeValue()); } else { boolVal = defaultValue; } } return boolVal; }
From source file:Main.java
/** * Checks in under a given root element whether it can find a child element * which matches the name supplied. Returns {@link Element} if exists. * /*from ww w . j av a 2s . com*/ * @param name the Element name (required) * @param root the parent DOM element (required) * * @return the Element if discovered */ public static Element findFirstElementByName(String name, Element root) { Assert.hasText(name, "Element name required"); Assert.notNull(root, "Root element required"); return (Element) root.getElementsByTagName(name).item(0); }
From source file:Main.java
public static HashMap<String, String> getElementsByTagNameHashMap(String sTag, Element eElement) { String value = null;//w ww.j a v a2 s . com HashMap<String, String> map = new HashMap<String, String>(); NodeList nlList = eElement.getElementsByTagName(sTag); if (nlList != null && nlList.item(0) != null) { NodeList nodes = nlList.item(0).getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); map.put(node.getNodeName(), node.getTextContent()); } } return map; }
From source file:Main.java
/** * Returns a child element with a given name from an XML element. * //from w w w . j a va2 s . c om * @param element * A Element * @param tagName * The name of the child element * * @return The child element or <code>null</code> if no such child exists */ public static Element getChildElement(Element element, String tagName) { if (element == null) { return null; } NodeList list = element.getElementsByTagName(tagName); if (list != null && list.getLength() > 0) { return (Element) list.item(0); } return null; }