List of utility methods to do XML Document to String Convert
String | toXML(Document document) Serializes a DOM Document to XML DOMImplementationLS domLS = (DOMImplementationLS) implementation;
LSSerializer serializer = domLS.createLSSerializer();
String s = serializer.writeToString(document);
return s;
|
String | getXml(Document doc) get Xml TransformerFactory transfac = TransformerFactory.newInstance(); Transformer trans = transfac.newTransformer(); trans.setOutputProperty(OutputKeys.METHOD, "xml"); trans.setOutputProperty(OutputKeys.INDENT, "yes"); trans.setOutputProperty( "{http://xml.apache.org/xslt}indent-amount", Integer.toString(2)); StringWriter sw = new StringWriter(); ... |
int | numResults(Document doc) num Results Node results = doc.getDocumentElement(); int res = -1; try { res = Integer.valueOf(results.getAttributes() .getNamedItem("count").getNodeValue()); } catch (Exception e) { res = -1; return res; |
String | documentToString(Node n) document To String StringBuilder buffer = new StringBuilder(); if (n == null) return ""; if (n instanceof Document) { buffer.append(documentToString((n).getFirstChild())); } else if (n instanceof Element) { Element element = (Element) n; buffer.append("<"); ... |
String | getErrorText(Document xml) get Error Text NodeList errorNodes = xml.getElementsByTagName("error"); if (errorNodes.getLength() > 0) { Element errorElement = (Element) errorNodes.item(0); return getElementText(errorElement); return null; |
String | xmlDocumentToString(Document document) xml Document To String return docToString(document.getChildNodes()).trim();
|
String | xmlToString(Document doc) xml To String final StringWriter sw = new StringWriter(); TransformerFactory transformerFactory = TransformerFactory .newInstance(); try { Transformer transformer = transformerFactory.newTransformer(); transformer.transform(new DOMSource(doc), new StreamResult(sw)); return sw.toString(); } catch (Exception e) { ... |