Java XML String Transform parseEventsToXML(String module, Hashtable keyvalues)

Here you can find the source of parseEventsToXML(String module, Hashtable keyvalues)

Description

parse Events To XML

License

Open Source License

Declaration

@SuppressWarnings("unchecked")
    public static String parseEventsToXML(String module, Hashtable keyvalues) 

Method Source Code

//package com.java2s;
/**//from  www. ja  v a 2 s.  co m
* BigBlueButton open source conferencing system - http://www.bigbluebutton.org/
*
* Copyright (c) 2010 BigBlueButton Inc. and by respective authors (see below).
*
* This program is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* Foundation; either version 2.1 of the License, or (at your option) any later
* version.
*
* BigBlueButton is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along
* with BigBlueButton; if not, see <http://www.gnu.org/licenses/>.
* 
*/

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Hashtable;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
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.Element;

public class Main {
    @SuppressWarnings("unchecked")
    public static String parseEventsToXML(String module, Hashtable keyvalues) {
        DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder;
        try {
            docBuilder = dbfac.newDocumentBuilder();
            Document doc = docBuilder.newDocument();

            Element node = doc.createElement(module);

            ArrayList keysarray = new ArrayList(keyvalues.keySet());
            for (int i = 0; i < keysarray.size(); i++) {
                String key = (String) keysarray.get(i);
                node.setAttribute(key, keyvalues.get(key) + "");
            }
            doc.appendChild(node);

            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");

            //initialize StreamResult with File object to save to file
            StreamResult result = new StreamResult(new StringWriter());
            DOMSource source = new DOMSource(doc);
            transformer.transform(source, result);

            String xmlString = result.getWriter().toString();

            return xmlString;
        } catch (Exception e) {

        }

        return "";
    }
}

Related

  1. identity(String xml)
  2. indentXML(String fileContent)
  3. loadSourceFromURL(String systemID)
  4. mergeXml(String... xmlSources)
  5. normXML(String s)
  6. parseToString(final Node node)
  7. parseXmlFile(String filename, boolean validating, boolean namespaceAware)
  8. readFile(String fileName)
  9. readFile(String systemId)