List of utility methods to do XML String Transform
StreamResult | getStreamResultForFile(String filename) get Stream Result For File if (filename == null || "".equals(filename)) throw new IllegalArgumentException("Filename for result can't be null or empty."); return new StreamResult(new File(filename)); |
String | getString(Node node, boolean indent) Return the contents of the node as a string. Transformer transformer; try { transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, (indent) ? "yes" : "no"); StreamResult result = new StreamResult(new StringWriter()); DOMSource source = new DOMSource(node); transformer.transform(source, result); ... |
String | getStringFromStreamSource(StreamSource src) Convert the input from the provided Reader into a String . inputNotNull(src); InputStream inputStream = src.getInputStream(); if (inputStream != null) { return getStringFromStream(inputStream); } else { final Reader r = src.getReader(); return getStringFromReader(r); |
Templates | getTemplates(String name) get Templates Templates tpl = (Templates) templates.get(name); if (tpl == null) { tpl = loadTemplates(name); templates.put(name, tpl); return tpl; |
Validator | getValidator(String path) get Validator String xsdUri = XMLConstants.W3C_XML_SCHEMA_NS_URI; SchemaFactory sf = SchemaFactory.newInstance(xsdUri); InputStream stream = new FileInputStream(path); StreamSource xsd = new StreamSource(stream); Schema schema = sf.newSchema(xsd); return schema.newValidator(); |
String | getXmlStringFromSource(Source payload) Method to get formatted Xml string from Xml source String result = null; StreamResult strResult = new StreamResult(new StringWriter()); if (payload != null) { try { TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); ... |
String | identity(String xml) Executes an identity transform. xml = removeBOM(xml); if (xml != null) xml = xml.trim(); if ((xml == null) || (xml.length() == 0)) { throw new TransformerException("Empty XML."); StringWriter result = new StringWriter(); transform(new StreamSource(new StringReader(xml)), new StreamResult(result), false); ... |
String | indentXML(String fileContent) Indents the given XML file content. final String res; StreamResult xmlOutput = null; try { Transformer transformer; Source xmlInput = new StreamSource(new StringReader(fileContent)); xmlOutput = new StreamResult(new StringWriter()); transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); ... |
Source | loadSourceFromURL(String systemID) load Source From URL Source targetXSD; try { URL targetURL = new URL(systemID); InputStream is = targetURL.openStream(); targetXSD = new StreamSource(is); targetXSD.setSystemId(targetURL.toExternalForm()); } catch (IOException e) { e.printStackTrace(); ... |
String | mergeXml(String... xmlSources) Merges many Serialized XML Strings into one following the MergeDocument schema. String merge = "<merge:root xmlns:merge=\"http://org.jboss.tusk.esb.support.soa/merged\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"; for (String s : xmlSources) { merge = merge + "<source>"; merge = merge + s; merge = merge + "</source>"; merge += "</merge:root>"; return merge; ... |