List of usage examples for org.w3c.dom Node getNodeValue
public String getNodeValue() throws DOMException;
From source file:io.wcm.testing.mock.osgi.OsgiMetadataUtil.java
public static Map<String, Object> getProperties(Document document) { Map<String, Object> props = new HashMap<>(); if (document != null) { try {/*from w w w .jav a 2 s . c o m*/ XPath xpath = XPATH_FACTORY.newXPath(); xpath.setNamespaceContext(NAMESPACE_CONTEXT); NodeList nodes = (NodeList) xpath.evaluate( "/components/component[1]/property[@name!='' and @value!='']", document, XPathConstants.NODESET); if (nodes != null) { for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); String name = node.getAttributes().getNamedItem("name").getNodeValue(); String value = node.getAttributes().getNamedItem("value").getNodeValue(); String type = null; Node typeAttribute = node.getAttributes().getNamedItem("type"); if (typeAttribute != null) { type = typeAttribute.getNodeValue(); } if (StringUtils.equals("Integer", type)) { props.put(name, Integer.parseInt(value)); } else { props.put(name, value); } } } } catch (XPathExpressionException ex) { throw new RuntimeException("Error evaluating XPath.", ex); } } return props; }
From source file:XmlUtil.java
/** * Returns a map of all node's attributes. All non-attribute nodes are ignored. *///from w w w . jav a 2s . c o m public static Map<String, String> getAllAttributes(Node node) { HashMap<String, String> attrs = new HashMap<String, String>(); NamedNodeMap nmm = node.getAttributes(); for (int j = 0; j < nmm.getLength(); j++) { Node attribute = nmm.item(j); if (attribute.getNodeType() != Node.ATTRIBUTE_NODE) { continue; } attrs.put(attribute.getNodeName(), attribute.getNodeValue()); } return attrs; }
From source file:Main.java
/** * Serializes the DOM-tree to a stringBuffer. Recursive method! * * @param node Node to start examining the tree from. * @param writeString The StringBuffer you want to fill with xml. * @return The StringBuffer containing the xml. * /*from w w w .j av a 2 s. c o m*/ * @since 2002-12-12 * @author Mattias Bogeblad */ public static StringBuffer serializeDom(Node node, StringBuffer writeString) { int type = node.getNodeType(); try { switch (type) { // print the document element case Node.DOCUMENT_NODE: { writeString.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); writeString = serializeDom(((Document) node).getDocumentElement(), writeString); break; } // print element with attributes case Node.ELEMENT_NODE: { writeString.append("<"); writeString.append(node.getNodeName()); NamedNodeMap attrs = node.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { Node attr = attrs.item(i); String outString = " " + attr.getNodeName() + "=\"" + replaceSpecialCharacters(attr.getNodeValue()) + "\""; writeString.append(outString); } writeString.append(">"); NodeList children = node.getChildNodes(); if (children != null) { int len = children.getLength(); for (int i = 0; i < len; i++) writeString = serializeDom(children.item(i), writeString); } break; } // handle entity reference nodes case Node.ENTITY_REFERENCE_NODE: { String outString = "&" + node.getNodeName() + ";"; writeString.append(outString); break; } // print cdata sections case Node.CDATA_SECTION_NODE: { String outString = "<![CDATA[" + node.getNodeValue() + "]]>"; writeString.append(outString); break; } // print text case Node.TEXT_NODE: { writeString.append(replaceSpecialCharacters(node.getNodeValue())); break; } // print processing instruction case Node.PROCESSING_INSTRUCTION_NODE: { String data = node.getNodeValue(); String outString = "<?" + node.getNodeName() + " " + data + "?>"; writeString.append(outString); break; } } if (type == Node.ELEMENT_NODE) { String outString = "</" + node.getNodeName() + ">"; writeString.append(outString); } } catch (Exception e) { } return writeString; }
From source file:Main.java
private static void serializeElement(StringBuilder sb, Element element, int tabIndex) { sb.append('\n'); for (int i = 0; i < tabIndex; i++) { sb.append('\t'); }//w w w.java2s . co m sb.append("<"); sb.append(element.getTagName()); if (element.hasAttributes()) { NamedNodeMap attributes = element.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { Node attribute = attributes.item(i); sb.append(" "); sb.append(attribute.getNodeName()); sb.append("="); sb.append("\""); String value = attribute.getNodeValue(); sb.append(value.replace("\"", "\\\"")); sb.append("\""); } } sb.append(">"); NodeList nodeList = element.getChildNodes(); ArrayList<Element> childElements = new ArrayList<Element>(); for (int i = 0; i < nodeList.getLength(); i++) { Node childNode = nodeList.item(i); if (childNode instanceof Element) { childElements.add((Element) childNode); } } if (childElements.size() == 0) { sb.append(escapeInvalidCharacters(getTextContent(element))); } else { for (Element childElement : childElements) { serializeElement(sb, childElement, tabIndex + 1); } sb.append('\n'); for (int i = 0; i < tabIndex; i++) { sb.append('\t'); } } sb.append("</"); sb.append(element.getTagName()); sb.append(">"); }
From source file:DomUtil.java
/** * Get the trimed text content of a node or null if there is no text *//*from w w w .j a v a 2 s .co m*/ public static String getContent(Node n) { if (n == null) return null; Node n1 = DomUtil.getChild(n, Node.TEXT_NODE); if (n1 == null) return null; String s1 = n1.getNodeValue(); return s1.trim(); }
From source file:cz.muni.fi.webmias.TeXConverter.java
/** * Converts TeX formula to MathML using LaTeXML through a web service. * * @param query String containing one or more keywords and TeX formulae * (formulae enclosed in $ or $$)./* ww w . java 2 s .c o m*/ * @return String containing formulae converted to MathML that replaced * original TeX forms. Non math tokens are connected at the end. */ public static String convertTexLatexML(String query) { query = query.replaceAll("\\$\\$", "\\$"); if (query.matches(".*\\$.+\\$.*")) { try { HttpClient httpclient = HttpClients.createDefault(); HttpPost httppost = new HttpPost(LATEX_TO_XHTML_CONVERSION_WS_URL); // Request parameters and other properties. List<NameValuePair> params = new ArrayList<>(1); params.add(new BasicNameValuePair("code", query)); httppost.setEntity(new UrlEncodedFormEntity(params, StandardCharsets.UTF_8)); // Execute and get the response. HttpResponse response = httpclient.execute(httppost); if (response.getStatusLine().getStatusCode() == 200) { HttpEntity resEntity = response.getEntity(); if (resEntity != null) { try (InputStream responseContents = resEntity.getContent()) { DocumentBuilder dBuilder = MIaSUtils.prepareDocumentBuilder(); org.w3c.dom.Document doc = dBuilder.parse(responseContents); NodeList ps = doc.getElementsByTagName("p"); String convertedMath = ""; for (int k = 0; k < ps.getLength(); k++) { Node p = ps.item(k); NodeList pContents = p.getChildNodes(); for (int j = 0; j < pContents.getLength(); j++) { Node pContent = pContents.item(j); if (pContent instanceof Text) { convertedMath += pContent.getNodeValue() + "\n"; } else { TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(); StringWriter buffer = new StringWriter(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.transform(new DOMSource(pContent), new StreamResult(buffer)); convertedMath += buffer.toString() + "\n"; } } } return convertedMath; } } } } catch (TransformerException | SAXException | ParserConfigurationException | IOException ex) { Logger.getLogger(ProcessServlet.class.getName()).log(Level.SEVERE, null, ex); } } return query; }
From source file:Main.java
/** * Find all attribute in a node with specific prefix. * * @param node the note that contains the attributes * @param prefix the prefix to search for * @param removePrefix true: removes the prefix from attribute name (name is the key of resulting map) * @return the values or a empty map/* www .j a v a 2s .c o m*/ */ public static HashMap<String, String> getAttributes(Node node, String prefix, boolean removePrefix) { final HashMap<String, String> result = new HashMap<>(); final NamedNodeMap attributes = node.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { final Node att = attributes.item(i); String name = att.getNodeName(); if (name.startsWith(prefix)) { if (removePrefix) { name = name.substring(prefix.length()); } result.put(name, att.getNodeValue()); } } return result; }
From source file:Main.java
/** * Sarches for ressources that have to be downloaded and creates a list with all download links. * * @param node to check for download links * @return list with all found download links *//*ww w . j ava 2 s . c om*/ private static ArrayList<String> findUrls(Node node) { int type = node.getNodeType(); ArrayList<String> result = new ArrayList<>(); String temp = null; NamedNodeMap atts = node.getAttributes(); Log.i(TAG, "parsing for ressources. node: " + node.getNodeName() + " value: " + node.getNodeValue() + " atts length: " + atts.getLength() + "type: " + type); switch (type) { //Element case Node.ELEMENT_NODE: //TODO: This method is stupid. It just looks for // attributes named ressourcepath. What if we have to download several ressources? for (int j = 0; j < atts.getLength(); j++) { Log.i(TAG, "atts: " + atts.item(j).getNodeName() + " value: " + atts.item(j).getNodeValue()); temp = atts.item(j).getNodeValue(); if (temp != null) { result.add(temp); Log.i(TAG, "added path: " + temp); } Log.i(TAG, "parent node:" + node.getParentNode().getNodeName()); Element parent = (Element) node.getParentNode(); parent.setAttribute("TESTITEST", "dllink"); } // get the pages, means the children for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { ArrayList<String> childres = findUrls(child); if (childres.size() > 0) { result.addAll(childres); } } break; } return result; }
From source file:Main.java
public static String nodeToString(org.w3c.dom.Node domNode) { if (domNode == null) { return ""; }// w w w . j av a 2 s . co m String s = typeName[domNode.getNodeType()]; String nodeName = domNode.getNodeName(); if (!nodeName.startsWith("#")) { s += ": " + nodeName; } if (domNode.getNodeValue() != null) { if (s.startsWith("ProcInstr")) s += ", "; else s += ": "; // Trim the value to get rid of NL's at the front String t = domNode.getNodeValue().trim(); int x = t.indexOf("\n"); if (x >= 0) t = t.substring(0, x); s += t; } return s; }
From source file:Main.java
/** * Return the text (node value) of the first node under this, works best if normalized. *//* w w w . j a v a 2 s .c o m*/ public static String elementValue(Element element) { if (element == null) return null; // make sure we get all the text there... element.normalize(); Node textNode = element.getFirstChild(); if (textNode == null) return null; StringBuffer valueBuffer = new StringBuffer(); do { if (textNode.getNodeType() == Node.CDATA_SECTION_NODE || textNode.getNodeType() == Node.TEXT_NODE) { valueBuffer.append(textNode.getNodeValue()); } } while ((textNode = textNode.getNextSibling()) != null); return valueBuffer.toString(); }