List of utility methods to do XML Transform
Document | transformStringToDoc(String input) transform String To Doc ByteArrayInputStream bais = new ByteArrayInputStream(input.getBytes()); Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(bais); return doc; |
XMLGregorianCalendar | transformStringToXMLGregorianCalendar(String dateTime, String pattern) transform String To XML Gregorian Calendar XMLGregorianCalendar xmlGregorianCalendar; SimpleDateFormat format = new SimpleDateFormat(pattern); try { xmlGregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(); Calendar calendar = Calendar.getInstance(); calendar.setLenient(true); calendar.setTime(format.parse(dateTime)); xmlGregorianCalendar.setYear(calendar.get(Calendar.YEAR)); ... |
String | transformToHTML(StreamSource xmlStreamSource) transform To HTML String htmlResult = null; try { StringWriter writer = new StringWriter(); StringReader xsltReader = new StringReader(XSLT_TO_HTML); TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory .newTransformer(new javax.xml.transform.stream.StreamSource(xsltReader)); transformer.transform(xmlStreamSource, new javax.xml.transform.stream.StreamResult(writer)); ... |
void | transformToOutputStream(Node node, OutputStream os, boolean omitXmlDeclaration) transform To Output Stream if (node.getNodeType() == Node.TEXT_NODE) { byte[] bytes = tranformTextNodeToByteArray(node); os.write(bytes); } else { transformNonTextNodeToOutputStream(node, os, omitXmlDeclaration); |
StringBuffer | transformToString(Source xmlSource, Source xslSource) transform To String StringWriter writer = new StringWriter(); Transformer transformer; try { if (xslSource == null) { transformer = TransformerFactory.newInstance().newTransformer(); } else { transformer = TransformerFactory.newInstance().newTransformer(xslSource); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.transform(xmlSource, new StreamResult(writer)); return writer.getBuffer(); } catch (Exception e) { e.printStackTrace(); return writer.getBuffer(); } finally { try { writer.close(); } catch (Exception e) { e.printStackTrace(); |
String | transformViaXslt(String xml, String xslFile) transform Via Xslt TransformerFactory tFactory = TransformerFactory.newInstance(); StringReader xmlreader = new StringReader(xml); Transformer transformer = tFactory.newTransformer(new StreamSource(new File(xslFile))); StringWriter output = new StringWriter(); transformer.transform(new StreamSource(xmlreader), new StreamResult(output)); return output.getBuffer().toString(); |
StringBuffer | transformXml(final StreamSource xslSrc, final StreamSource docSrc, final Map params, final URIResolver resolver) Use the transform specified by xslSrc and transform the document specified by docSrc, and return the resulting document. StringBuffer sb = null; StringWriter writer = new StringWriter(); TransformerFactory tf = TransformerFactory.newInstance(); if (null != resolver) { tf.setURIResolver(resolver); Transformer t = tf.newTransformer(xslSrc); if (params != null) { ... |
String | transformXML(Reader xml, Reader xsl) transforms an xml document using the specified xsl stylesheet if (xsl != null) { TransformerFactory tFactory = TransformerFactory.newInstance(); Source xmlSource = new StreamSource(xml); Source xslSource = new StreamSource(xsl); Transformer transformer = tFactory.newTransformer(xslSource); StringWriter outputWriter; transformer.transform(xmlSource, new StreamResult(outputWriter = new StringWriter())); return outputWriter.getBuffer().toString(); ... |
ByteArrayOutputStream | transformXmlByXslt(StreamSource source, URI xslFile) Transforms a xml file by xslt and returns the result as string. StreamSource xsltSource = new StreamSource(xslFile.getPath()); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(xsltSource); try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { StreamResult result = new StreamResult(outputStream); transformer.transform(source, result); return outputStream; |
void | write_plain_content_tag(String name, String content, AttributesImpl atts, TransformerHandler hd) Writes a normal tag containing a text only. if (name != null && content != null) { atts.clear(); hd.startElement("", "", name, atts); hd.characters(content.toCharArray(), 0, content.length()); hd.endElement("", "", name); |