Java Utililty Methods XML Node to String

List of utility methods to do XML Node to String

Description

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

Method

StringtoXMLString(Node node)
Convert an XML node to a string.
Transformer transformer = null;
try {
    transformer = tFactory.newTransformer();
} catch (TransformerConfigurationException e) {
    e.printStackTrace();
DOMSource source = new DOMSource(node);
StringWriter xmlWriter = new StringWriter();
...
StringtoXMLString(Node node, boolean header)
Returns a String representation of the DOM hierarchy rooted at the argument Node.
try {
    Source source = new DOMSource(node);
    StringWriter stringWriter = new StringWriter();
    Result result = new StreamResult(stringWriter);
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();
    if (!header)
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
...
voidwriteXml(Node n, OutputStream os)
write Xml
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
t.setOutputProperty(OutputKeys.INDENT, "yes");
t.transform(new DOMSource(n), new StreamResult(os));
Stringxml(Node node)
xml
TransformerFactory factory = TransformerFactory.newInstance();
return xml(factory, node);
StringXML2String(Node node)
XML String
try {
    StringWriter writer = new StringWriter();
    Transformer trans = TransformerFactory.newInstance().newTransformer();
    trans.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    Source source = new DOMSource(node);
    Result result = new StreamResult(writer);
    trans.transform(source, result);
    return writer.toString();
...
byte[]xmlToBytes(final Node body)
xml To Bytes
final Transformer transformer = FACTORY.newTransformer();
final Source source = new DOMSource(body);
final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
final Result result = new StreamResult(buffer);
transformer.transform(source, result);
return buffer.toByteArray();
SourceXmlToSource(Node node)
Xml To Source
Source source = new DOMSource(node);
return source;
voidxmlToStream(Node n, OutputStream os)
xml To Stream
Source source = new DOMSource(n);
Result result = new StreamResult(os);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.transform(source, result);
voidxmlToStreamE(Node n, OutputStream os)
xml To Stream E
try {
    Source source = new DOMSource(n);
    Result result = new StreamResult(os);
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.transform(source, result);
} catch (IllegalArgumentException | TransformerException e) {
    throw new Error(e);
...
StringxmlToString(Node doc)
xml To String
try {
    DOMSource domSource = new DOMSource(doc);
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.transform(domSource, result);
...