Java XML String Transform getOutputSQLPageXML(String inStatement, Map map)

Here you can find the source of getOutputSQLPageXML(String inStatement, Map map)

Description

get Output SQL Page XML

License

Open Source License

Declaration

public static String getOutputSQLPageXML(String inStatement, Map map) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2005 Exadel Inc and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * //from w w w . j  av  a  2  s.  c om
 * Contributors:
 *     Exadel Inc - initial API and implementation
 *******************************************************************************/

import java.io.StringWriter;
import java.util.Iterator;
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.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Attr;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Element;

public class Main {
    public static String getOutputSQLPageXML(String inStatement, Map map) {
        // tau 27.06.04 06.07.04

        org.w3c.dom.Document doc = null;
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = null;
        try {
            builder = factory.newDocumentBuilder();
        } catch (ParserConfigurationException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        DOMImplementation impl = builder.getDOMImplementation();
        doc = impl.createDocument(null, "SQLPage", null);

        Element root = doc.getDocumentElement();

        for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
            Object element = it.next();
            if (element instanceof Map.Entry) {
                String key = (String) ((Map.Entry) element).getKey();
                String value = (String) ((Map.Entry) element).getValue();
                Attr attr = doc.createAttribute(key);
                attr.setValue(value);
                root.setAttributeNode(attr);
            }
        }

        /*
          String userid = "";
          String driver = "";
          String url = "";
          String password = "";
              
          if (rdbConnection != null) {
        userid = rdbConnection.getUserName();
        driver = rdbConnection.getDriverClassName();
        url = rdbConnection.getURL();
        password = rdbConnection.getPassword();
          }
              
          Attr attr = doc.createAttribute("userid");
          attr.setValue(userid);
          root.setAttributeNode(attr);
              
          attr = doc.createAttribute("driver");
          attr.setValue(driver);
          root.setAttributeNode(attr);
              
          attr = doc.createAttribute("url");
          attr.setValue(url);
          root.setAttributeNode(attr);
              
          attr = doc.createAttribute("password");
          attr.setValue(password);
          root.setAttributeNode(attr);
              
          */

        Element item = doc.createElement("Statement"); // Create element        

        if (inStatement == null)
            inStatement = "";
        item.appendChild(doc.createCDATASection(inStatement));
        root.appendChild(item);

        StringWriter stringOut = new StringWriter(); //Writer will be a String

        //       Serialisation through Tranform. add tau 06.04.07
        DOMSource domSource = new DOMSource(doc);

        StreamResult streamResult = new StreamResult(stringOut);

        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer serializer = null;

        try {
            serializer = tf.newTransformer();
        } catch (TransformerConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //serializer.setOutputProperty(OutputKeys.ENCODING,"ISO-8859-1");
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");

        try {
            serializer.transform(domSource, streamResult);
        } catch (TransformerException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }

        return stringOut.toString();
    }
}

Related

  1. getHtmlAsString(Node node)
  2. getIntegerAttribute(XMLStreamReader in, String attribute)
  3. getNode(String text)
  4. getNotFoundXmlForObjectId(final String pid, final String detailCode, final String description)
  5. getOutputMimeType(String xslName)
  6. getPropertyAsString(final Properties prop, final String comment)
  7. getSchema(String schemaString)
  8. getSchemaFromResource(String... files)
  9. getSourceAsString(Source source)