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

Stringformat(Document doc)
format
OutputFormat format = new OutputFormat(doc);
format.setEncoding("UTF-8");
format.setIndenting(true);
format.setIndent(2);
format.setOmitXMLDeclaration(true);
ByteArrayOutputStream out = new ByteArrayOutputStream();
Writer output = new OutputStreamWriter(out);
XMLSerializer serializer = new XMLSerializer(output, format);
...
Stringformat(Document doc)
format
OutputFormat format = new OutputFormat(doc);
format.setEncoding("UTF-8");
format.setIndenting(true);
format.setIndent(2);
format.setOmitXMLDeclaration(true);
ByteArrayOutputStream out = new ByteArrayOutputStream();
Writer output = new OutputStreamWriter(out);
XMLSerializer serializer = new XMLSerializer(output, format);
...
Stringformat(Document document)
formats XML with proper indentation
OutputFormat format = new OutputFormat(document);
format.setLineWidth(65);
format.setIndenting(true);
format.setIndent(2);
Writer out = new StringWriter();
XMLSerializer serializer = new XMLSerializer(out, format);
serializer.serialize(document);
return out.toString();
...
Documentformat(Source xslSource, Document xml, URIResolver resolver)
format
TransformerFactory factory = TransformerFactory.newInstance();
DOMSource xmlSource = new DOMSource(xml);
Templates t = factory.newTemplates(xslSource);
Transformer transformer = t.newTransformer();
transformer.setURIResolver(resolver);
Document resultDoc = newDocument();
DOMResult result = new DOMResult(resultDoc);
transformer.transform(xmlSource, result);
...
voidformatElementEnd( Document document, Element element, String formatString)
format Element End
Node lastChild = element.getLastChild();
if (lastChild != null) {
    if (lastChild instanceof Element) {
        element.appendChild(document.createTextNode(formatString));
    } else if (lastChild instanceof Text) {
        Text textNode = (Text) lastChild;
        String text = textNode.getWholeText();
        if (hasOnlyTextSiblings(textNode)) {
...
voidformatElementStart( Document document, Element element, String formatString)
format Element Start
Node previousSibling = element.getPreviousSibling();
if (previousSibling == null || previousSibling instanceof Element) {
    element.getParentNode().insertBefore(document.createTextNode(formatString), element);
} else if (previousSibling instanceof Text) {
    Text textNode = (Text) previousSibling;
    String text = textNode.getWholeText();
    if (!formatString.equals(text)) {
        textNode.replaceWholeText(trimRight(text) + formatString);
...
voidformatNode(Document doc, Node parent, Node node)
format Node
Node txt = doc.createTextNode("\n    ");
parent.insertBefore(txt, node);
NodeList children = node.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
    txt = doc.createTextNode("\n      ");
    node.insertBefore(txt, children.item(i));
    i += 1;
txt = doc.createTextNode("\n    ");
node.appendChild(txt);
StringformatPython(int start, Document doc, String s)
format Python
int pos = 0;
int prev;
boolean check = false;
String spaces = "";
String formattedLine = "";
String scannedLine = "";
Scanner src = new Scanner(doc.getText(0, doc.getLength()));
try {
...
voidformatXML(Document document, org.w3c.dom.Element root, String tab)
format XML into human readable form
NodeList children = root.getChildNodes();
Node[] nodes = new Node[children.getLength()];
for (int i = 0; i < children.getLength(); i++)
    nodes[i] = children.item(i);
for (int i = 0; i < nodes.length; i++) {
    root.insertBefore(document.createTextNode("\n" + tab), nodes[i]);
    if (nodes[i] instanceof org.w3c.dom.Element)
        formatXML(document, (org.w3c.dom.Element) nodes[i], "  " + tab);
...
DocumentformatXML(Document xml, Document xsl)
format XML
TransformerFactory factory = TransformerFactory.newInstance();
DOMSource xslSource = new DOMSource(xsl);
Templates t = factory.newTemplates(xslSource);
Transformer transformer = t.newTransformer();
return formatXML(xml, transformer);