List of utility methods to do XML Element to String
String | extractText(Element obj, String localName) extract Text NodeList nodelist = obj.getElementsByTagNameNS("*", localName); if (nodelist != null && nodelist.getLength() > 0) { Element node = (Element) nodelist.item(0); if (node != null) { String str = node.getTextContent(); if (str != null) { str = str.trim(); if (str.length() > 0) ... |
String | extractTextElementValue(final Element textElement) Trivial convenience method to extract the value of a (text) Element, coping with the possibility that text child Nodes may not have been coalesced. NodeList childNodes = textElement.getChildNodes(); String result; if (childNodes.getLength() == 1) { result = ensureExtractTextNodeValue(childNodes.item(0)); } else { StringBuilder resultBuilder = new StringBuilder(); for (int i = 0; i < childNodes.getLength(); i++) { resultBuilder.append(ensureExtractTextNodeValue(childNodes.item(i))); ... |
InputStream | getElementAsInputStream(Element xml) get Element As Input Stream Transformer transformer = TransformerFactory.newInstance().newTransformer(); ByteArrayOutputStream out = new ByteArrayOutputStream(); StreamResult res = new StreamResult(out); transformer.transform(new DOMSource(xml), res); return new ByteArrayInputStream(out.toByteArray()); |
String | getElementText(Element e) get Element Text final StringBuilder val = new StringBuilder(); for (Node n = e.getFirstChild(); n != null; n = n.getNextSibling()) { if (n.getNodeType() == Node.TEXT_NODE || n.getNodeType() == Node.CDATA_SECTION_NODE) { val.append(n.getNodeValue()); return val.toString(); |
String | getNamedElemXml(Element parent, String elementName) get Named Elem Xml String val = null; NodeList list = parent.getElementsByTagName(elementName); if (list.getLength() > 0) { val = getXmlString(list.item(0)); return val; |
String | getString(Element el, String label) get String NodeList l = el.getElementsByTagName(label); if (l.getLength() < 1) return null; Node n = l.item(0).getFirstChild(); return n == null ? "" : n.getNodeValue(); |
String | getString(Element element) get String StringBuffer buffer = new StringBuffer(); _getString(element, buffer); return (new String(buffer)); |
String | getString(Element element, String expr) get String try { XPath xPath = XPathFactory.newInstance().newXPath(); return xPath.evaluate(expr + "/child::text()", element, XPathConstants.STRING).toString(); } catch (Exception e) { return ""; |
String | getString(Element element, String name, String def) Utility to get a String attribute from the given DOM Element. if (!element.hasAttribute(name)) { return def; return element.getAttribute(name); |
String | getString(Element root, String name) get String try { Element e = (Element) root.getElementsByTagName(name).item(0); return e.getTextContent(); } catch (Exception e) { System.err.println("Failed to parse String for tag " + name + "!"); return "NOT SET"; |