Java XML Format formatAttributes(Node node)

Here you can find the source of formatAttributes(Node node)

Description

Returns formatted attributes of the node.

License

Mozilla Public License

Parameter

Parameter Description
node The node.

Return

Formatted attributes.

Declaration

public static String formatAttributes(Node node) 

Method Source Code

//package com.java2s;
/**/*from   w  ww.j  av  a2  s  .  c o  m*/
 * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
 * If a copy of the MPL was not distributed with this file, You can obtain one at
 * http://mozilla.org/MPL/2.0/.
 * 
 * This Source Code Form is also subject to the terms of the Health-Related Additional
 * Disclaimer of Warranty and Limitation of Liability available at
 * http://www.carewebframework.org/licensing/disclaimer.
 */

import java.io.StringWriter;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

public class Main {
    /**
     * Returns formatted attributes of the node.
     * 
     * @param node The node.
     * @return Formatted attributes.
     */
    public static String formatAttributes(Node node) {
        StringBuilder sb = new StringBuilder();
        NamedNodeMap attrs = node.getAttributes();

        for (int i = 0; i < attrs.getLength(); i++) {
            Node attr = attrs.item(i);
            sb.append(' ').append(attr.getNodeName()).append("= '").append(attr.getNodeValue()).append("'");
        }

        return sb.toString();
    }

    /**
     * Converts an XML document to a formatted XML string.
     * 
     * @param doc The document to format.
     * @return Formatted XML document.
     */
    public static String toString(Document doc) {
        if (doc == null) {
            return "";
        }

        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.OMIT_XML_DECLARATION, "yes");
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.transform(domSource, result);
            return writer.toString();
        } catch (Exception e) {
            return e.toString();
        }
    }
}

Related

  1. applyTransformation(InputStream xsltStream, Map xsltParameters, InputStream inputXmlStream, OutputStream outputStream)
  2. format(final String xml)
  3. format(Node node)
  4. format(Node node, String indent)
  5. format(String unformattedXml)
  6. formattedPrint(Node xml, OutputStream out)
  7. formatXML(byte[] xmlData, int indent)
  8. formatXML(String unformatted)
  9. formatXml(String xml)