Java Utililty Methods XML Document Format

List of utility methods to do XML Document Format

Description

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

Method

voidprintDoc(Document doc)
print Doc
try {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = tf.newTransformer();
    t.transform(new DOMSource(doc), new StreamResult(System.out));
} catch (Exception ex) {
    new RuntimeException(ex);
voidprintDoc(Document doc, PrintStream pstrm)
print Doc
String version = "1.0"; 
String encoding = null; 
pstrm.print("<?xml version=\"" + version + "\"");
if (encoding == null) {
    pstrm.println("?>");
} else {
    pstrm.println(" encoding=\"" + encoding + "\"?>");
printPI(doc, pstrm);
pstrm.println("");
printDoctype(doc, pstrm);
pstrm.println("\n");
printElem(doc.getDocumentElement(), "", true, pstrm);
voidprintDoctype(Document doc, PrintStream pstrm)
print Doctype
if (doc.getDoctype() != null) {
    pstrm.println("<!DOCTYPE " + doc.getDoctype().getName());
    String pubid = doc.getDoctype().getPublicId();
    if (pubid != null) {
        pstrm.print("\tPUBLIC \"" + pubid + "\"");
    String sysid = doc.getDoctype().getSystemId();
    if (sysid != null) {
...
voidprintDocument(Document d)
Prints the specified document.
printNode(d.getDocumentElement(), 0);
StringprintDocument(Document doc, boolean prettyPrint)
print Document
ByteArrayOutputStream baos = new ByteArrayOutputStream();
printDocument(doc, baos, prettyPrint);
return new String(baos.toByteArray());
voidprintDocument(Document doc, OutputStream out)
print Document
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); 
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", "4"); 
transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "UTF-8"))); 
...
voidprintDocument(Document doc, Writer ps)
Print the XML document
try {
    String lineSeparator = System.getProperty("line.separator");
    ps.write("<?xml version=\"1.0\"?>");
    ps.write(lineSeparator);
    ps.write(lineSeparator);
    Element node = doc.getDocumentElement();
    printTree(ps, node, "", lineSeparator);
} catch (IOException e) {
...
voidprintDocument(Document document)
prints the given document to system.out
try {
    transformer.transform(new DOMSource(document), new StreamResult(System.out));
} catch (TransformerException e) {
    System.err.println("printDocument failed cause of " + e);
voidprintDocument(Document document)
Print a DOM document to stdout.
serializeDocument(document, System.out);
StringprintDocument(Document document)
print Document
StringWriter writer = new StringWriter();
try {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 
    transformer.transform(new DOMSource(document), new StreamResult(writer));
    return writer.toString();
} finally {
...