List of usage examples for org.w3c.dom Node getFirstChild
public Node getFirstChild();
From source file:Main.java
public static String getTextValue(final Element element, final String tagName) { Node n = getNode(element, tagName); if (n != null) { Node child = n.getFirstChild(); return (child == null) ? "" : child.getNodeValue(); }/*w w w. j a v a 2s. c o m*/ return null; }
From source file:Main.java
public static void processNode(Node node) throws Exception { if (node.hasChildNodes()) { for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { processNode(child);/*from www . ja v a 2s .c om*/ } } else { node.setTextContent(" "); } }
From source file:Main.java
/** * Gets a node value by name//from w w w.j a v a 2s. c om * * @param parent the parent node * @param nodeName the node name * @return String the value of the node * @throws Exception */ public static String getChildNodeValueByName(Node parent, String nodeName) throws Exception { Node node = findChildNodeByName(parent, nodeName); if (node != null && node.getFirstChild() != null) { return node.getFirstChild().getNodeValue().trim(); } return ""; }
From source file:Main.java
/** * Extracts an Integer from a document that consists of an Integer only. * /*from ww w. j av a 2 s . co m*/ * @param doc * @return the Integer */ public static int extractInt(Node doc) { if (doc == null) { return 0; } return Integer.parseInt(doc.getFirstChild().getTextContent()); }
From source file:Main.java
private static void updateNode(String nodeName, String value) throws Exception { File f = new File(System.getProperty("user.dir") + "\\DBConf.xml"); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(f); Node tempElem = doc.getElementsByTagName(nodeName).item(0); tempElem.getFirstChild().setNodeValue(value); doc.getElementsByTagName("conf").item(0).appendChild(tempElem); DOMSource source = new DOMSource(doc); StreamResult res = new StreamResult(new FileOutputStream(f)); TransformerFactory.newInstance().newTransformer().transform(source, res); }
From source file:Main.java
/** * Extracts the text from inside the given node * @param node The node to extract from// ww w. ja v a 2s . c o m * @param defaultValue The value to return if no text is found, or it is empty * @return the extracted text */ public static String getNodeTextValue(final Node node, final String defaultValue) { try { String text = node.getFirstChild().getNodeValue(); if (text == null || text.trim().isEmpty()) return defaultValue; return text.trim(); } catch (Exception ex) { return defaultValue; } }
From source file:Main.java
public static Map<String, String> grabProperty(Map<String, String> map, Element elem) { for (Entry<String, String> entry : map.entrySet()) { String key = entry.getKey(); String val = ""; NodeList nodeList = elem.getElementsByTagName(key); if (nodeList == null || nodeList.getLength() <= 0) { //entry.setValue(""); } else {// w w w. j ava2 s .c o m Node node = nodeList.item(0); if (node != null) node = node.getFirstChild(); if (node != null) val = node.getNodeValue(); //String val=nodeList.item(0).getFirstChild().getNodeValue(); //entry.setValue(""); } entry.setValue(val); } return map; }
From source file:Main.java
public static Set<Node> findChildElementsByTag(Node node, String tag) { final Set<Node> result = new LinkedHashSet<>(); for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { if (tag.equals(child.getNodeName())) { result.add(child);/*from www .ja v a 2 s .c om*/ } else if (child.hasChildNodes()) { result.addAll(findChildElementsByTag(child, tag)); } } return result; }
From source file:Main.java
/** Finds and returns the first child node with the given name. */ public static Element getFirstChildElement(Node parent, String elemNames[]) { // search for node Node child = parent.getFirstChild(); while (child != null) { if (child.getNodeType() == Node.ELEMENT_NODE) { for (int i = 0; i < elemNames.length; i++) { if (child.getNodeName().equals(elemNames[i])) { return (Element) child; }// w w w . j a va 2s . c o m } } child = child.getNextSibling(); } // not found return null; }
From source file:Main.java
/** * based on public Java5 javadoc of org.w3c.dom.Node.setTextContent method *///from ww w.j a v a 2 s . co m public static void setTextContent(Node node, final String text) { while (node.hasChildNodes()) { node.removeChild(node.getFirstChild()); } if (text != null && text.length() > 0) { Node textNode = node.getOwnerDocument().createTextNode(text); node.appendChild(textNode); } }