List of usage examples for org.w3c.dom Node getNodeValue
public String getNodeValue() throws DOMException;
From source file:edu.duke.cabig.c3pr.webservice.integration.XMLUtils.java
private static boolean isIgnorableWhitespace(Node node) { return node.getNodeType() == Node.TEXT_NODE && StringUtils.isBlank(node.getNodeValue()); }
From source file:Main.java
public static Object transformXmlNodesIntoMap(Node node) { Map<String, Object> nodeMap = new HashMap<String, Object>(); NodeList subNodes = node.getChildNodes(); NamedNodeMap nodeAttrs = node.getAttributes(); for (int nodeAttrIdx = 0; nodeAttrIdx < nodeAttrs.getLength(); nodeAttrIdx++) { Node attrNode = nodeAttrs.item(nodeAttrIdx); // nodeMap.put("@"+attrNode.getNodeName(), // attrNode.getTextContent()); nodeMap.put("@" + attrNode.getNodeName(), attrNode.getNodeValue()); }/* w w w.jav a 2 s . co m*/ if (nodeAttrs.getLength() == 0) if (subNodes.getLength() == 0) return ""; else if (subNodes.getLength() == 1 && subNodes.item(0).getNodeType() == Node.TEXT_NODE) // return subNodes.item(0).getTextContent(); return subNodes.item(0).getNodeValue(); for (int subNodeIdx = 0; subNodeIdx < subNodes.getLength(); subNodeIdx++) { Node subNode = subNodes.item(subNodeIdx); if (subNode.getNodeType() == Node.TEXT_NODE) { // nodeMap.put(subNode.getNodeName(), subNode.getTextContent()); nodeMap.put(subNode.getNodeName(), subNode.getNodeValue()); } else { if (nodeMap.containsKey(subNode.getNodeName())) { Object subObject = nodeMap.get(subNode.getNodeName()); if (subObject instanceof List<?>) { ((List<Object>) subObject).add(transformXmlNodesIntoMap(subNode)); } else { List<Object> subObjectList = new ArrayList<Object>(); subObjectList.add(subObject); subObjectList.add(transformXmlNodesIntoMap(subNode)); nodeMap.put(subNode.getNodeName(), subObjectList); } } else { nodeMap.put(subNode.getNodeName(), transformXmlNodesIntoMap(subNode)); } } } return nodeMap; }
From source file:Main.java
/** * Whenever you'd need to print a configuration node and/or its children. * * @param root the root node to print./* w ww . j a va2 s. c o m*/ * @param out the print stream that should be used to outpu * @param recurse boolean * @param prefix String */ public static void printChildElements(Element root, PrintStream out, boolean recurse, String prefix) { out.print(prefix + "<" + root.getNodeName()); NamedNodeMap attrs = root.getAttributes(); Node node; for (int i = 0; i < attrs.getLength(); i++) { node = attrs.item(i); out.print(" " + node.getNodeName() + "=\"" + node.getNodeValue() + "\""); } out.println(">"); String data = getText(root); if (data != null && data.trim().length() > 0) out.println(prefix + "\t" + data); data = getCData(root); if (data != null && data.trim().length() > 0) out.println(prefix + "\t<![CDATA[" + data + "]]>"); NodeList nodes = root.getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { node = nodes.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { if (recurse) printChildElements((Element) node, out, recurse, prefix + "\t"); else out.println(prefix + node.getNodeName()); } } out.println(prefix + "</" + root.getNodeName() + ">"); }
From source file:Main.java
/** * Removes text nodes that are only containing whitespace characters * inside a DOM tree.//from w w w . j a v a2 s . c o m * * @param element the root node to normalize. */ public static void stripWhitespaceNodes(Node element) { Node node, child; for (child = element.getFirstChild(); child != null; child = node) { node = child.getNextSibling(); stripWhitespaceNodes(child); } if (element.getNodeType() == Node.TEXT_NODE && element.getNodeValue().trim().length() == 0) { element.getParentNode().removeChild(element); } }
From source file:Main.java
/** * Returns the text associated to the given node * i.e. the value of the//w ww. j a v a 2 s . c o m * @param node * @return */ public static String getText(Node node) { Node textNode = node.getFirstChild(); if (textNode == null) { return null; } short nodeType = textNode.getNodeType(); if (nodeType != Node.TEXT_NODE && nodeType != Node.CDATA_SECTION_NODE) { return null; } return textNode.getNodeValue().trim(); }
From source file:XmlUtil.java
/** * Returns value of single child text node or <code>null</code>. *///from w w w .j a va 2s . c om public static String getChildTextNodeValue(Node node) { if (node.getChildNodes().getLength() != 1) { return null; } Node item0 = node.getChildNodes().item(0); if (item0.getNodeType() != Node.TEXT_NODE) { return null; } return item0.getNodeValue(); }
From source file:Main.java
static public String getNodeValue(Element parent, String nodeName) { NodeList nodes = parent.getElementsByTagName(nodeName); if (nodes.getLength() == 0) return null; Element curElem = (Element) nodes.item(0); Node textNode = curElem.getFirstChild(); if (textNode != null) { String encVal = curElem.getAttribute("enc"); String charSetVal = curElem.getAttribute("charSet"); String returnVal = textNode.getNodeValue(); if (encVal != null && encVal.equals("t")) { if (charSetVal == null) return (URLDecoder.decode(returnVal)); else//from www . j a v a 2s .c o m try { return (URLDecoder.decode(returnVal, charSetVal)); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return (returnVal); } else return (""); }
From source file:Main.java
private static void removeEmptyTextNodes(Node parentNode) { Node childNode = parentNode.getFirstChild(); while (childNode != null) { Node nextChild = childNode.getNextSibling(); short nodeType = childNode.getNodeType(); if (nodeType == Node.TEXT_NODE) { boolean containsOnlyWhitespace = childNode.getNodeValue().trim().isEmpty(); if (containsOnlyWhitespace) { parentNode.removeChild(childNode); }//from w ww .j a v a 2 s . c o m } childNode = nextChild; } }
From source file:Main.java
public static ArrayList<HashMap<String, String>> getWhoList(Node currentNode, String tagName) { String result = ""; ArrayList<HashMap<String, String>> whoArrayList = new ArrayList<HashMap<String, String>>(); NodeList childNodeList = currentNode.getChildNodes(); for (int i = 0; i < childNodeList.getLength(); i++) { Node childNode = childNodeList.item(i); if (childNode.getNodeName().equals(tagName)) { HashMap<String, String> whoMap = new HashMap<String, String>(); NamedNodeMap attrNodeMap = childNode.getAttributes(); Node activity = attrNodeMap.getNamedItem("activity"); Node email = attrNodeMap.getNamedItem("email"); Node name = attrNodeMap.getNamedItem("name"); if (activity != null) { whoMap.put("activity", activity.getNodeValue()); }//from w w w .ja v a 2 s. co m if (email != null) { whoMap.put("email", email.getNodeValue()); } if (name != null) { whoMap.put("name", name.getNodeValue()); } whoArrayList.add(whoMap); } } return whoArrayList; }
From source file:Main.java
/** * Get textvalue from element. Loop all #text chidlnoes. * This is used by getText() methods, endusers usually * should not call this directly./* w w w . ja v a2 s. com*/ */ private static String getSimpleText(Element element) { if (element == null) return null; StringBuilder sb = new StringBuilder(); NodeList nodes = element.getChildNodes(); Node node; for (int i = 0; i < nodes.getLength(); i++) { node = nodes.item(i); if (node.getNodeType() == Node.TEXT_NODE) sb.append(node.getNodeValue()); } return sb.toString().trim(); }