List of usage examples for org.w3c.dom Node getNodeValue
public String getNodeValue() throws DOMException;
From source file:Main.java
/** * Returns the concatenated child text of the specified node. * This method only looks at the immediate children of type * Node.TEXT_NODE or the children of any child * node that is of type Node.CDATA_SECTION_NODE * for the concatenation.// w w w .j av a2 s . co m * * @param node The node to look at. */ public static String getChildText(Node node) { // is there anything to do? if (node == null) { return null; } // concatenate children text StringBuffer str = new StringBuffer(); Node child = node.getFirstChild(); while (child != null) { short type = child.getNodeType(); if (type == Node.TEXT_NODE) { str.append(child.getNodeValue()); } else if (type == Node.CDATA_SECTION_NODE) { str.append(getChildText(child)); } child = child.getNextSibling(); } // return text value return str.toString(); }
From source file:Main.java
/** * Method to get the value of "Value" node *///from w ww . j a v a 2s .co m public static String getValueOfValueNode(Node n) { NodeList textNodes = n.getChildNodes(); Node textNode; StringBuffer value = new StringBuffer(""); for (int j = 0; j < textNodes.getLength(); j++) { textNode = textNodes.item(j); value.append(textNode.getNodeValue()); } return (value.toString().trim()); }
From source file:Main.java
public static String getElementText(Element ele) { // is there anything to do? if (ele == null) { return null; }/*from ww w . j av a 2 s.c om*/ // get children text Node child = ele.getFirstChild(); if (child != null) { short type = child.getNodeType(); if (type == Node.TEXT_NODE) { return child.getNodeValue(); } } // return text value return null; }
From source file:Main.java
public static String logElement(Element El, int level) { String Es = ""; String indentText = " "; String addIndT = ""; // add indent int ind = 0;/*from www. jav a 2 s . c o m*/ while (ind < level) { addIndT = addIndT + indentText; ind++; } String name = El.getNodeName(); Es = "\n" + addIndT + "<" + name + " "; // Attribs NamedNodeMap namedNodeMap = El.getAttributes(); StringBuilder sb = new StringBuilder(Es); for (int i = 0; i < namedNodeMap.getLength(); i++) { Attr att = (Attr) namedNodeMap.item(i); sb.append(" " + Es + att.getName() + "=\"" + att.getNodeValue() + "\" "); // Es = " " + Es + att.getName() + "=\"" + att.getNodeValue() + // "\" "; } sb.append(">"); // Es = Es + ">"; Es = sb.toString(); NodeList pl = El.getChildNodes(); int index = pl.getLength(); // text nodes if (index > 0) { int i = 0; while (i < index) { Node DomNode = pl.item(i); if ((DomNode.getNodeType()) == org.w3c.dom.Node.TEXT_NODE) { String Etext = DomNode.getNodeValue(); Es = Es + "\n " + addIndT + addIndT + Etext; } i++; } } // Child Elements if (index > 0) { level++; int i = 0; while (i < index) { Node DomNode = pl.item(i); if ((DomNode.getNodeType()) == org.w3c.dom.Node.ELEMENT_NODE) { Element el = (Element) DomNode; Es = Es + logElement(el, level); } i++; } } Es = Es + "\n" + addIndT + "</" + name + ">"; return Es; }
From source file:Main.java
public static String lookupNamespaceURI(Node root, String specifiedPrefix) { if (root == null) { return null; }//from ww w . jav a2s . c o m if (root.hasAttributes()) { NamedNodeMap nnm = root.getAttributes(); for (int i = 0; i < nnm.getLength(); i++) { Node n = nnm.item(i); if (("xmlns".equals(n.getPrefix()) && specifiedPrefix.equals(n.getNodeName())) || ("xmlns:" + specifiedPrefix).equals(n.getNodeName())) { return n.getNodeValue(); } } } return lookupNamespaceURI(root.getParentNode(), specifiedPrefix); }
From source file:Main.java
/** * Method to convert a NODE XML to a string. * @param n node XML to input.//w w w .j a v a2s . co m * @return string of the node n. */ public static String convertElementToString(Node n) { String name = n.getNodeName(); short type = n.getNodeType(); if (Node.CDATA_SECTION_NODE == type) { return "<![CDATA[" + n.getNodeValue() + "]]>"; } if (name.startsWith("#")) { return ""; } StringBuilder sb = new StringBuilder(); sb.append('<').append(name); NamedNodeMap attrs = n.getAttributes(); if (attrs != null) { for (int i = 0; i < attrs.getLength(); i++) { Node attr = attrs.item(i); sb.append(' ').append(attr.getNodeName()).append("=\"").append(attr.getNodeValue()).append("\""); } } String textContent; NodeList children = n.getChildNodes(); if (children.getLength() == 0) { if ((textContent = n.getTextContent()) != null && !"".equals(textContent)) { sb.append(textContent).append("</").append(name).append('>'); //; } else { sb.append("/>").append('\n'); } } else { sb.append('>').append('\n'); boolean hasValidChildren = false; for (int i = 0; i < children.getLength(); i++) { String childToString = convertElementToString(children.item(i)); if (!"".equals(childToString)) { sb.append(childToString); hasValidChildren = true; } } if (!hasValidChildren && ((textContent = n.getTextContent()) != null)) { sb.append(textContent); } sb.append("</").append(name).append('>'); } return sb.toString(); }
From source file:Main.java
public static Map<String, Object> convertNodeToMap(Node node) { NodeList nodeList = node.getChildNodes(); Map<String, Object> map = new HashMap<String, Object>(nodeList.getLength()); for (int i = 0; i < nodeList.getLength(); i++) { Node nodec = nodeList.item(i); String key = nodec.getNodeName(); Object value = null;/* w w w. j ava 2 s. co m*/ if (nodec.hasChildNodes()) { NodeList nodeListc = nodec.getChildNodes(); if (nodeListc.getLength() == 1) { Node noded = nodeListc.item(0); short type = noded.getNodeType(); if (type == 3 || type == 4) { value = noded.getNodeValue(); } if (noded.getNodeType() == 1) { value = convertNodeToMap(nodec); } } else { value = convertNodeToMap(nodec); } } map.put(key, value); } return map; }
From source file:org.aectann.postage.TrackingStatusRefreshTask.java
private static String getFirstVaueOrNull(Document document, String tag) { NodeList elements = document.getElementsByTagName(tag); String result = null;/*from ww w .j a va 2 s.c o m*/ if (elements.getLength() > 0) { Node firstChild = elements.item(0).getFirstChild(); String nodeValue; if (firstChild != null && (nodeValue = firstChild.getNodeValue()) != null && nodeValue.length() > 0) { result = nodeValue; } } return result; }
From source file:Main.java
public static String getAttrsAsString(Node node) { if (node == null) return ""; NamedNodeMap attrs = node.getAttributes(); StringBuilder val = new StringBuilder(); if (attrs != null) { for (int i = 0; i < attrs.getLength(); i++) { Node nval = attrs.item(i); if (nval != null) { if (i > 0) val.append(", "); val.append(nval.getNodeName()).append("=").append(nval.getNodeValue()); }/*from w w w . jav a 2 s .co m*/ } } return val.toString().trim(); }
From source file:com.tieuluan.struts2.utils.AppSetting.java
/** * Gets the node value./*from w ww. j a v a 2 s .co m*/ * * @param contextNode * the context node * @param xpath * the xpath * @return the node value */ private static String getNodeValue(Node contextNode, String xpath) { String val = null; try { Node node = XPathAPI.selectSingleNode(contextNode, xpath); val = node.getNodeValue(); } catch (TransformerException ex) { val = null; } log.toString(); return val; }