List of utility methods to do XML String to Document
Document | toXMLDocument(String xmlString) to XML Document return toXMLDocument(new InputSource(new StringReader(xmlString))); |
String | writeDocumentToString(Document document) write Document To String try { TransformerFactory transfac = TransformerFactory.newInstance(); Transformer trans; trans = transfac.newTransformer(); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); trans.setOutputProperty(OutputKeys.INDENT, "yes"); trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); StringWriter writer = new StringWriter(); ... |
Document | xml2Document(String xml) xml Document try { DocumentBuilder builder = DOCUMENT_BUILDER_FACTORY.get().newDocumentBuilder(); return builder.parse(new ByteArrayInputStream(xml.getBytes(DEFAULT_CHARSET))); } catch (ParserConfigurationException ex) { throw new RuntimeException(ex); } catch (SAXException ex) { throw new RuntimeException(ex); } catch (UnsupportedEncodingException ex) { ... |
Element | xmlNewDocument(final String tagName) Xml new document. final Document doc = XML_BUILDER.get().newDocument(); final Element tag = doc.createElement(tagName); return tag; |
Document | xmlStringToDocument(String xml) Converts an XML string into an equivalent XML document. return DocumentBuilderFactory.newInstance().newDocumentBuilder() .parse(new InputSource(new StringReader(xml))); |
Document | xmlStringToDocument(String xmlInputString) xml String To Document DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); Document dom = null; try { DocumentBuilder db = dbf.newDocumentBuilder(); InputSource input = new InputSource(new StringReader(xmlInputString)); dom = db.parse(input); } catch (ParserConfigurationException pce) { pce.printStackTrace(); ... |