Java Utililty Methods XML Format

List of utility methods to do XML Format

Description

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

Method

StringprettyXml(String xml)
pretty Xml
TransformerFactory tfactory = TransformerFactory.newInstance();
Transformer serializer;
try {
    Source source = new StreamSource(new StringReader(xml));
    StringWriter writer = new StringWriter();
    serializer = tfactory.newTransformer();
    serializer.setOutputProperty(OutputKeys.INDENT, "yes");
    serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
...
StringprettyXml(String xml)
pretty Xml
Transformer serializer;
try {
    Source source = new StreamSource(new StringReader(xml));
    StringWriter writer = new StringWriter();
    serializer = transFactory.newTransformer();
    serializer.setOutputProperty(OutputKeys.INDENT, "yes");
    serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    serializer.transform(source, new StreamResult(writer));
...
StringprettyXml(String xml)
Uses the Xalan javax.transform classes to indent an XML string properly so that it is easier to read.
StringWriter out = new StringWriter();
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
StreamResult result = new StreamResult(out);
...
voidprettyXmlPrint(Node xml, OutputStream out)
pretty Xml Print
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.transform(new DOMSource(xml), new StreamResult(out));
voidprint(Element elm, OutputStream out)
Print part of document
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(elm);
StreamResult result = new StreamResult(out);
transformer.transform(source, result);
voidprint(File file)
print
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
transformer.transform(new StreamSource(file), new StreamResult(System.out));
voidprint(Node node)
Print a DOM to standard out.
if (node == null) {
    System.out.println("DOMUtils.print: Null node");
    return;
try {
    DOMSource ds = new DOMSource(node);
    StreamResult result = new StreamResult(System.out);
    TransformerFactory transFact = TransformerFactory.newInstance();
...
Stringprint(Node node)
print
try {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();
    transformer.setOutputProperty("omit-xml-declaration", "yes");
    DOMSource source = new DOMSource(node);
    ByteArrayOutputStream os = new ByteArrayOutputStream(2000);
    StreamResult result = new StreamResult(os);
    transformer.transform(source, result);
...
voidprint(Node node, OutputStream out)
print
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
transformer.transform(new DOMSource(node), new StreamResult(new OutputStreamWriter(out, "UTF-8")));
...
Stringprint(Node node, String encoding)
Prints a Node tree recursively.
if (node == null) {
    return null;
try {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();
    transformer.setOutputProperty("omit-xml-declaration", "yes");
    transformer.setOutputProperty("encoding", encoding);
...