List of usage examples for org.w3c.dom Node getNodeValue
public String getNodeValue() throws DOMException;
From source file:Main.java
public static void getAttributes(Node p_node, HashMap<String, String> p_map) throws Exception { NamedNodeMap l_nnm = null;/* w w w . j a va 2 s.co m*/ Node l_n = null; String l_an = null; String l_av = null; l_nnm = p_node.getAttributes(); if (l_nnm != null) { for (int l_i = 0; l_i < l_nnm.getLength(); l_i++) { l_n = l_nnm.item(l_i); l_an = l_n.getNodeName(); l_av = l_n.getNodeValue(); p_map.put(l_an, l_av); } } }
From source file:Main.java
/** * _more_/* w w w . ja v a2s . c o m*/ * * @param html _more_ * @param node _more_ */ public static void toHtml(StringBuffer html, Node node) { switch (node.getNodeType()) { case Node.ELEMENT_NODE: { NodeList children = node.getChildNodes(); int numChildren = children.getLength(); html.append("<b>" + node.getNodeName().replace("_", " ") + "</b>"); html.append(": "); for (int i = 0; i < numChildren; i++) { Node child = children.item(i); if (((child.getNodeType() == Node.TEXT_NODE) || (child.getNodeType() == Node.CDATA_SECTION_NODE))) { String v = child.getNodeValue(); if (v == null) { continue; } if (v.trim().length() == 0) { continue; } html.append(v); html.append(" "); } } boolean didone = false; NamedNodeMap nnm = node.getAttributes(); if (nnm != null) { for (int i = 0; i < nnm.getLength(); i++) { Attr attr = (Attr) nnm.item(i); String attrName = attr.getNodeName(); if (attrName.startsWith("xmlns") || attrName.startsWith("xsi:")) { continue; } if (!didone) { html.append("<ul>"); didone = true; } html.append(attrName.replace("_", " ") + "=" + attr.getNodeValue()); html.append("<br>\n"); } } int cnt = 0; for (int i = 0; i < numChildren; i++) { Node child = children.item(i); if (((child.getNodeType() == Node.TEXT_NODE) || (child.getNodeType() == Node.CDATA_SECTION_NODE))) { continue; } if (!didone) { html.append("<ul>"); didone = true; } if (cnt > 0) { html.append("<br>"); } toHtml(html, child); cnt++; } if (didone) { html.append("</ul>"); } break; } } }
From source file:org.owasp.benchmark.tools.BenchmarkCrawler.java
public static List<Node> getNamedChildren(String name, List<Node> list) { List<Node> results = new ArrayList<Node>(); for (Node n : list) { NodeList children = n.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if (child.getNodeName().equals(name)) { System.out.println("> " + child.getNodeName() + "::" + child.getNodeValue()); results.add(child);// ww w. j a va 2 s . c om } } } return results; }
From source file:Main.java
public static void removeNamespaceDeclarations(Node node, String... namespaces) { NamedNodeMap namedNodeMap = ((Element) node).getAttributes(); for (int nameIndex = 0; nameIndex < namedNodeMap.getLength(); nameIndex++) { Node namedNode = namedNodeMap.item(nameIndex); String uri = namedNode.getNamespaceURI(); String localName = namedNode.getLocalName(); if (uri != null && uri.equals("http://www.w3.org/2000/xmlns/")) { for (String removeableNamespace : namespaces) { if (namedNode.getNodeValue().equals(removeableNamespace)) { ((Element) node).removeAttributeNS("http://www.w3.org/2000/xmlns/", localName); nameIndex--;//from w w w . j ava2 s. com } } } } }
From source file:Main.java
public static StringBuffer gatherTextPCDATAAndCDATADescendants(Node node, StringBuffer sb, String separator) { if (sb == null) sb = new StringBuffer(); int t = node.getNodeType(); if (t == Node.CDATA_SECTION_NODE || t == Node.TEXT_NODE) { //sb.append("================= "+node.getNodeName()+" ================="); sb.append(node.getNodeValue()); } else {//from w ww . j a v a 2 s . c o m NodeList list = node.getChildNodes(); for (int i = 0, len = list.getLength(); i < len; i++) { Node n = list.item(i); gatherTextPCDATAAndCDATADescendants(n, sb, separator); if (i < len - 1) sb.append(separator); } } return sb; }
From source file:cz.incad.kramerius.virtualcollections.VirtualCollectionsManager.java
public static VirtualCollection doVC(String pid, FedoraAccess fedoraAccess, ArrayList<String> languages) { try {//from ww w. ja v a 2s . c om String xPathStr; XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); XPathExpression expr; ArrayList<String> langs = new ArrayList<String>(); if (languages == null || languages.isEmpty()) { String[] ls = KConfiguration.getInstance().getPropertyList("interface.languages"); for (int i = 0; i < ls.length; i++) { String lang = ls[++i]; langs.add(lang); } } else { langs = new ArrayList<String>(languages); } String name = ""; boolean canLeave = true; fedoraAccess.getDC(pid); Document doc = fedoraAccess.getDC(pid); xPathStr = "//dc:title/text()"; expr = xpath.compile(xPathStr); Node node = (Node) expr.evaluate(doc, XPathConstants.NODE); if (node != null) { name = StringEscapeUtils.escapeXml(node.getNodeValue()); } xPathStr = "//dc:type/text()"; expr = xpath.compile(xPathStr); node = (Node) expr.evaluate(doc, XPathConstants.NODE); if (node != null) { canLeave = Boolean.parseBoolean(StringEscapeUtils.escapeXml(node.getNodeValue())); } VirtualCollection vc = new VirtualCollection(name, pid, canLeave); for (String lang : langs) { String dsName = TEXT_DS_PREFIX + lang; String value = IOUtils.readAsString(fedoraAccess.getDataStream(pid, dsName), Charset.forName("UTF8"), true); vc.addDescription(lang, value); } return vc; } catch (Exception vcex) { logger.log(Level.WARNING, "Could not get virtual collection for " + pid + ": " + vcex.toString()); return null; } }
From source file:Main.java
/** * Retrieves the text of a given element. * //w ww .j a v a 2s .co m * @param elem the Element for which the text value is requested * @return the text value of that element or null if the element has no text value */ static public String getElementText(Node elem) { String value = null; Node node = (elem != null) ? elem.getFirstChild() : null; // Find Text while (node != null) { // Find all Text nodes if (node.getNodeType() == Node.TEXT_NODE) { // set or append if (value == null) value = node.getNodeValue(); else value += node.getNodeValue(); } node = node.getNextSibling(); } return value; }
From source file:Main.java
/** * get the value of an Attribute in the Xml Document. * finds the first occurance of the parent element and then searches its attributes * for the occurance of the attribute and retrieves its value. * @param root the root Element./*from w w w . jav a 2s. c o m*/ * @param elemName the name of the element to search for. * @param att the name of the attribute to search for. * @return String the attribute value or null if not found. */ public static String getElementAttribute(Element root, String elemName, String att) { NodeList nl = root.getElementsByTagName(elemName); if (null == nl) { return (null); } Node n = nl.item(0); if (null == n) { return (null); } NamedNodeMap attributes = n.getAttributes(); if (null == attributes) { return (null); } n = attributes.getNamedItem(att); if (null == n) { return (null); } return (n.getNodeValue().trim()); }
From source file:Main.java
static public String getNodeValue(Node node) { if (node == null) return null; if (node.getNodeType() == Node.ELEMENT_NODE) return getElementText((Element) node); else if (node.getNodeType() == Node.DOCUMENT_FRAGMENT_NODE) return getFragmentText((DocumentFragment) node); else/*from w w w . jav a2 s. c om*/ return node.getNodeValue(); }
From source file:com.cloud.hypervisor.kvm.resource.LibvirtDomainXMLParser.java
private static String getTagValue(String tag, Element eElement) { NodeList tagNodeList = eElement.getElementsByTagName(tag); if (tagNodeList == null || tagNodeList.getLength() == 0) { return null; }/*from w ww . j av a 2 s .co m*/ NodeList nlList = tagNodeList.item(0).getChildNodes(); Node nValue = nlList.item(0); return nValue.getNodeValue(); }