Java Utililty Methods XML String Transform

List of utility methods to do XML String Transform

Description

The list of methods to do XML String Transform are organized into topic(s).

Method

StreamResultgetStreamResultForFile(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));
StringgetString(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);
...
StringgetStringFromStreamSource(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);
TemplatesgetTemplates(String name)
get Templates
Templates tpl = (Templates) templates.get(name);
if (tpl == null) {
    tpl = loadTemplates(name);
    templates.put(name, tpl);
return tpl;
ValidatorgetValidator(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();
StringgetXmlStringFromSource(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");
...
Stringidentity(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);
...
StringindentXML(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");
...
SourceloadSourceFromURL(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();
...
StringmergeXml(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;
...