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
/** * @param parent the parent XML element/* w w w .jav a 2s.c om*/ * @param childName the child node name * @return the list of child XML elements with specified node name */ public static List<Element> getChildElements(final Element parent, final String childName) { if (parent != null) { List<Element> result = new ArrayList<Element>(); NodeList nodes = parent.getElementsByTagName(childName); for (int i = 0, count = nodes.getLength(); i < count; i++) { result.add((Element) nodes.item(i)); } return result; } else { return null; } }
From source file:Main.java
public static Iterator getElementsByTagName(Element element, String tag) { ArrayList<Element> children = new ArrayList<Element>(); if (element != null && tag != null) { NodeList nodes = element.getElementsByTagName(tag); for (int i = 0; i < nodes.getLength(); i++) { Node child = nodes.item(i); children.add((Element) child); }/* w w w . j av a 2s. c o m*/ } return children.iterator(); }
From source file:Main.java
public static String getTextNodeValueByChildTagName(Element ele, String tagname) { String s = ""; try {/*from w w w .j a va2 s . c o m*/ NodeList nodes = ele.getElementsByTagName(tagname); if (nodes.getLength() > 0) { Node node = getTextNode(nodes.item(0)); if (node != null) { s = node.getNodeValue(); } } } catch (Exception e) { s = ""; } return s; }
From source file:Main.java
public static List<String> getNodeValues(String response, String tagName) throws ParserConfigurationException, SAXException { try {/*from ww w. jav a2 s .c om*/ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new ByteArrayInputStream(response.getBytes())); Element rootElement = document.getDocumentElement(); List<String> arrayList = new ArrayList<>(); NodeList list = rootElement.getElementsByTagName(tagName); NodeList subList; if (list != null && list.getLength() > 0) { for (int k = 0; k < list.getLength(); k++) { subList = list.item(k).getChildNodes(); if (subList != null && subList.getLength() > 0) { arrayList.add(subList.item(0).getNodeValue()); } } return arrayList; } } catch (Exception e) { return null; } return null; }
From source file:Main.java
public static Map<String, String> getConfigs(InputStream is) throws IOException, SAXException, ParserConfigurationException { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document dt = db.parse(is);//from w ww.j av a2s . c o m Element element = dt.getDocumentElement(); Map<String, String> config = new TreeMap<String, String>(); NodeList propertyList = element.getElementsByTagName("property"); int length = propertyList.getLength(); for (int i = 0; i < length; i++) { Node property = propertyList.item(i); String key = property.getChildNodes().item(0).getTextContent(); String value = property.getChildNodes().item(1).getTextContent(); config.put(key, value); } return config; }
From source file:Main.java
/** Given a person element, must get the element specified by the tagName, then must traverse that Node to get the value.//from ww w . jav a 2s . c o m Step1) get Element of name tagName from e Step2) cast element to Node and then traverse it for its non-whitespace, cr/lf value. Step3) return it! NOTE: Element is a subclass of Node @param e an Element @param tagName a tag name @return s the value of a Node */ public static String getValue(Element e, String tagName) { try { //get node lists of a tag name from a 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) { System.out.println(ex); ex.printStackTrace(); } return null; }
From source file:Main.java
/** * find the unique element, returns null if not found or if there is more than one * * @param tagName - tag name/*from w w w. j av a2s .c o m*/ * @param root - where to start looking * @return the element */ public static Element findElement(final String tagName, final Element root) { if (root.getTagName().equals(tagName)) { return root; } final NodeList nl = root.getElementsByTagName(tagName); if (nl != null && nl.getLength() == 1) { return (Element) nl.item(0); } return null; }
From source file:Main.java
/** * Create a child of the given node at the given index. * // w w w . ja v a2s .co m * @param doc * a document * @param element * an element * @param index * an index * @param nodeName * a node name * @return org.w3c.dom.Element */ public static Element createChildElement(Document doc, Element element, int index, String nodeName) { Element element2 = doc.createElement(nodeName); try { NodeList childList = element.getElementsByTagName(nodeName); Node child = childList.item(index); element.insertBefore(element2, child); } catch (Exception e) { element.appendChild(element2); } return element2; }
From source file:com.wso2telco.workflow.utils.WorkflowProperties.java
public static Map<String, String> loadWorkflowPropertiesFromXML() { if (propertiesMap == null) { try {//from www.j a v a 2 s . c o m propertiesMap = new HashMap<String, String>(); DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); String carbonHome = System.getProperty("carbon.home"); String workflowPropertiesFile = carbonHome + "/repository/conf/" + Constants.WORKFLOW_PROPERTIES_XML_FILE; Document document = builder.parse(new File(workflowPropertiesFile)); Element rootElement = document.getDocumentElement(); NodeList nodeList = rootElement.getElementsByTagName("Property"); if (nodeList != null && nodeList.getLength() > 0) { for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); String nodeName = node.getAttributes().getNamedItem("name").getNodeValue(); if (nodeName.equalsIgnoreCase(Constants.SERVICE_HOST) || nodeName.equalsIgnoreCase(Constants.KEY_WORKFLOW_EMAIL_NOTIFICATION_HOST) || nodeName.equalsIgnoreCase(Constants.KEY_WORKFLOW_EMAIL_NOTIFICATION_FROM_ADDRESS) || nodeName .equalsIgnoreCase(Constants.KEY_WORKFLOW_EMAIL_NOTIFICATION_FROM_PASSWORD) || nodeName.equalsIgnoreCase(Constants.PUBLISHER_ROLE_START_WITH) || nodeName.equalsIgnoreCase(Constants.PUBLISHER_ROLE_END_WITH) || nodeName.equalsIgnoreCase(Constants.MANDATE_SERVICE_HOST)) { String value = ((Element) node).getTextContent(); propertiesMap.put(nodeName, value); } else { //Not a matching property } } } } catch (Exception e) { String errorMessage = "Error in WorkflowProperties.loadWorkflowPropertiesFromXML"; log.error(errorMessage, e); } } else { //Return already loaded propertiesMap } return propertiesMap; }
From source file:Main.java
public static List<Element> getElementsByTagName(Element parent, String name, boolean localOnly) { List<Element> ret = new ArrayList<Element>(); if (!localOnly) { NodeList elementList = parent.getElementsByTagName(name); for (int i = 0; i < elementList.getLength(); i++) { ret.add((Element) elementList.item(i)); }//from w w w. j a va 2 s . c o m } else { NodeList childList = parent.getChildNodes(); for (int i = 0; i < childList.getLength(); i++) { if (childList.item(i).getNodeType() != Node.ELEMENT_NODE) continue; Element child = (Element) childList.item(i); if (child.getTagName().equals(name)) ret.add(child); } } return ret; }