Java XML String Transform getConfigurationAsXML(Map properties)

Here you can find the source of getConfigurationAsXML(Map properties)

Description

Get a set of properties as an XML Configuration

License

Apache License

Parameter

Parameter Description
properties The properties to convert to an XML Configuration

Exception

Parameter Description
TransformerException an exception
ParserConfigurationException an exception

Return

The XML description of the Configuration

Declaration

public static String getConfigurationAsXML(Map<String, Object> properties)
        throws TransformerException, ParserConfigurationException 

Method Source Code


//package com.java2s;
/*/*from   w w w.  j  a  va2  s . c  o  m*/
 * Copyright 2008 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import java.io.StringWriter;

import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

public class Main {
    /**
     * Get a set of properties as an XML Configuration
     * 
     * @param properties The properties to convert to an XML Configuration
     * @return The XML description of the Configuration
     * @throws TransformerException
     * @throws ParserConfigurationException
     */
    public static String getConfigurationAsXML(Map<String, Object> properties)
            throws TransformerException, ParserConfigurationException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.newDocument();
        Element entity = doc.createElement("entity");
        doc.appendChild(entity);

        for (Map.Entry<String, Object> element : properties.entrySet()) {
            String key = element.getKey();
            Object value = element.getValue();
            if (null == value) {
                continue;
            }

            if (value.getClass().isArray()) {
                Object[] valueObjects = (Object[]) value;
                for (int i = 0; i < valueObjects.length; i++) {
                    Element node = doc.createElement(key);
                    node.appendChild(doc.createTextNode(valueObjects[i] == null ? "" : valueObjects[i].toString()));
                    if (0 == i) {
                        node.setAttribute("repeatable", "true");
                    }
                    entity.appendChild(node);
                }
            } else {
                Element node = doc.createElement(key);
                node.appendChild(doc.createTextNode(value.toString()));
                entity.appendChild(node);
            }
        }
        return getDocumentAsXml(doc);
    }

    /**
     * Get a DOM Document as XML
     * 
     * @param doc The DOM document to convert
     * @return The XML description of the DOM
     * @throws TransformerException
     */
    public static String getDocumentAsXml(Document doc) throws TransformerException {
        DOMSource domSource = new DOMSource(doc);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        StringWriter sw = new StringWriter();
        StreamResult sr = new StreamResult(sw);
        transformer.transform(domSource, sr);
        String str = sw.toString();
        return str;
    }
}

Related

  1. extractElement(String xml, String tagName)
  2. extractText(Node n, StringBuffer buf)
  3. fileToXMLString(String filename)
  4. getAsXMLString(final Node node)
  5. getBeanDefinition(String beanId, File file)
  6. getHtmlAsString(Node node)
  7. getIntegerAttribute(XMLStreamReader in, String attribute)
  8. getNode(String text)
  9. getNotFoundXmlForObjectId(final String pid, final String detailCode, final String description)