Java tutorial
//package com.java2s; import java.io.StringWriter; import java.io.Writer; import java.util.Iterator; import java.util.Map; import javax.xml.transform.OutputKeys; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; public class Main { /** * Adds a new empty {@link Element} to the given parent {@link Element}. * * @param doc the document to add to. * @param parent the parent element. If {@code null} then the new child * will be added directly to the document. * @param name the name of the new element. * @return the created element. */ public static Element addElement(Document doc, Node parent, String name) { if (doc == null) { doc = parent.getOwnerDocument(); } final Element elem = doc.createElement(name); if (parent == null) { parent = doc; } parent.appendChild(elem); return elem; } /** * Adds a new {@link Element} with multiple attributes to the given parent * {@link Element}. The attribute data should be passed in via a {@link Map}, * with each entry containing an attribute name as key and attribute value as the value. * * @param doc * @param parent * @param name * @param attributeData * @return */ public static Element addElement(Document doc, Node parent, String name, Map<String, String> attributeData) { if (doc == null) { doc = parent.getOwnerDocument(); } final Element elem = addElement(doc, parent, name); if (null != attributeData && !attributeData.isEmpty()) { Iterator<Map.Entry<String, String>> iter = attributeData.entrySet().iterator(); while (iter.hasNext()) { Map.Entry<String, String> entry = iter.next(); elem.setAttribute(entry.getKey(), entry.getValue()); } } return elem; } /** * Adds a new {@link Element} with multiple attributes to the given parent * {@link Element}. The attribute data should be passed in pairs of key and * attribute value as the value. * * @param doc * @param parent * @param name * @param attributeData * @return */ public static Element addElement(Document doc, Node parent, String name, Object... attributeData) { if (doc == null) { doc = parent.getOwnerDocument(); } final Element elem = addElement(doc, parent, name); // check we have an even number of attribute pairs if ((attributeData.length & 1) == 1) { throw new RuntimeException("Need an even number attribute args to make attribute name/value pairs."); } addAttributes(elem, attributeData); return elem; } public static Element addAttributes(Element elem, Object... attributeData) { return addAttributes(elem, false, attributeData); } public static Element addAttributes(Element elem, boolean dropEmptyValues, Object... attributeData) { for (int i = 0; i < attributeData.length - 1; i += 2) { Object attribName = attributeData[i]; Object attribValue = attributeData[i + 1]; if (attribName != null && attribValue != null) { String value = attribValue.toString(); if (!(dropEmptyValues && value.isEmpty())) { elem.setAttribute(attribName.toString(), value); } } } return elem; } /** * Transforms a {@link Source} object into a {@link String} representation. * * @param xml the source input. * @return the string output. * @throws TransformerException */ public static String toString(Source xml) throws TransformerException { return toString(xml, false); } /** * Transforms a {@link Source} object into a {@link String} representation. * * @param xml the source input. * @param pretty if {@code true} then pretty print with white space. * @return the string output. * @throws TransformerException */ public static String toString(Source xml, boolean pretty) throws TransformerException { StringWriter writer = new StringWriter(); toString(xml, pretty, writer); return writer.toString(); } /** * Transforms a {@link Source} object into a {@link String} representation. * * @param xml the source input. * @param pretty if {@code true} then pretty print with white space. * @param writer write the string to this {@link Writer}. * @throws TransformerException */ public static void toString(Source xml, boolean pretty, Writer writer) throws TransformerException { final TransformerFactory factory = TransformerFactory.newInstance(); try { factory.setAttribute("{http://xml.apache.org/xalan}indent-amount", 2); } catch (IllegalArgumentException iae) { // try a different ident amount try { factory.setAttribute("indent-number", 2); } catch (IllegalArgumentException iae2) { // ignore } } final Transformer transformer = factory.newTransformer(); if (pretty) { transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); } transformer.transform(xml, new StreamResult(writer)); } }