Java Utililty Methods XML Transform Usage

List of utility methods to do XML Transform Usage

Description

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

Method

NodebytesToXml(final byte[] body)
bytes To Xml
final Transformer transformer = FACTORY.newTransformer();
final Source source = new StreamSource(new ByteArrayInputStream(body));
final DOMResult result = new DOMResult();
transformer.transform(source, result);
return result.getNode();
StreamSourcecastResultToSource(StreamResult result)
cast Result To Source
StreamSource ss = new StreamSource(new StringReader(result.getWriter().toString()));
String systemId = result.getSystemId();
if (systemId != null) {
    ss.setSystemId(new File(systemId));
return ss;
voidcloseSource(final Source source)
close Source
if (!(source instanceof StreamSource)) {
    return;
StreamSource stream = (StreamSource) source;
try {
    if (stream.getInputStream() != null) {
        stream.getInputStream().close();
} catch (IOException e) {
try {
    if (stream.getReader() != null) {
        stream.getReader().close();
} catch (IOException e) {
booleancompareSource(Source src, Source target)
compare Source
Transformer t1 = TransformerFactory.newInstance().newTransformer();
Transformer t2 = TransformerFactory.newInstance().newTransformer();
DOMResult srcResult = new DOMResult();
DOMResult tarResult = new DOMResult();
t1.transform(src, srcResult);
Node n1 = srcResult.getNode();
t2.transform(target, tarResult);
Node n2 = tarResult.getNode();
...
booleancompareStreamSource(StreamSource src1, StreamSource src2)
compare Stream Source
if (src1 == null || src2 == null) {
    System.out.println("compareStreamSource - src1 or src2 null!");
    return false;
InputStream is1 = src1.getInputStream();
InputStream is2 = src2.getInputStream();
if ((is1 == null) || (is2 == null)) {
    System.out.println("InputStream of - src1 or src2 null!");
...
Stringconvert(final Element element)
convert
if (null == element) {
    throw new NullPointerException("connot convert null DOM Element");
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();
StringWriter stringWriter = new StringWriter();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.transform(new DOMSource(element), new StreamResult(stringWriter));
...
StringconvertDisplay(Object object)
convert Display
try {
    String result;
    if (object instanceof ByteArrayInputStream) {
        InputStream is = (InputStream) object;
        byte[] data = new byte[is.available()];
        is.mark(0);
        is.read(data);
        is.reset();
...
InputStreamconvertMessageToInputStream(Source src)
convert Message To Input Stream
final Transformer transformer = TRANSFORMER_FACTORY.newTransformer();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
StreamResult result = new StreamResult(baos);
transformer.transform(src, result);
return new ByteArrayInputStream(baos.toByteArray());
DocumentBuilderFactorycreateAndGetFactory(Collection schemaSources)
create And Get Factory
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
factory.setIgnoringComments(true);
factory.setIgnoringElementContentWhitespace(true);
factory.setSchema(newSchema(schemaSources.toArray(new Source[schemaSources.size()])));
return factory;
PropertiescreateDefaultPropertiesForXML(boolean omitXMLDeclaration)
Method for static initializer
final Properties format = new Properties();
format.put(OutputKeys.METHOD, "xml");
format.put(OutputKeys.OMIT_XML_DECLARATION, (omitXMLDeclaration ? "yes" : "no"));
format.put(OutputKeys.INDENT, "yes");
return format;