title: Storing sql query in xml file
body: <p>I want to store sql query in tree format in xml file.I wrote a code which split the code into different tokens and store it in xml file.I am storing keywords like "select",from,where etc as xmlelements and the values of these keywords as textnodes inside corresponding elements.When the query contains single single statement it works as i need.but when nested query occurs i want to store the nested query inside "where" element.In that case the code didn't works properly.I am hereby attaching the code.
  Can anyone point out logical mistakes in my code????</p>
  
  <p>Second problem is that when i try to store the xmldata in "CreateFiles.xml" file ,the file is not displaying in the corresponding folder.what is the reason for that?</p>
  
  <p>CreateXml.java</p>
  
  <pre><code>    /*
   * To change this template, choose Tools | Templates
   * and open the template in the editor.
   */
  package Work;
  
  import java.io.File;
  import java.io.StringWriter;
  import java.util.ArrayList;
  import java.util.List;
  import java.util.Locale;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  import javax.xml.parsers.DocumentBuilder;
  import javax.xml.parsers.DocumentBuilderFactory;
  import javax.xml.parsers.ParserConfigurationException;
  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.Document;
  import org.w3c.dom.Element;
  
  /**
   *
   * @author user
   */
  public class Createxmls {
  
      public void CreateXML() throws ParserConfigurationException, TransformerConfigurationException, TransformerException {
          DocumentBuilderFactory documentFactory = DocumentBuilderFactory
                  .newInstance();
          DocumentBuilder documentBuilder = documentFactory
                  .newDocumentBuilder();
  
          Document document = documentBuilder.newDocument();
  
          Element rootElement = document.createElement("SQLStatement");
          document.appendChild(rootElement);
          Element stmt = document.createElement("SQLStatement");
          rootElement.appendChild(stmt);
          Element Element = document.createElement("null");
          String tokens[] = {"select", "max(id)", "from", "facedictionary", "where", "id = (",
              "select", "id", "from", "facedetails", "where", "id like 34 group by id)"};
          for (int i = 0; i &lt; tokens.length; i++) {
              System.out.println("tok:" + tokens[i]);
  
              if (tokens[i].equalsIgnoreCase("select") || tokens[i].equalsIgnoreCase("from") || tokens[i].equalsIgnoreCase("where")) {
  
                  Element = document.createElement(tokens[i]);
                  stmt.appendChild(Element);
              } else {
                  Element.appendChild(document.createTextNode(tokens[i]));
                  stmt.appendChild(Element);
              }
  
          }
  
          String xmlstring = "";
          TransformerFactory transformerFactory = TransformerFactory
                  .newInstance();
          Transformer transformer = transformerFactory.newTransformer();
          StreamResult streamResult = new StreamResult(new File(
                  "createFile.xml"));
          StringWriter sw = new StringWriter();
  
          Transformer serializer = TransformerFactory.newInstance().newTransformer();
  
          serializer.transform(new DOMSource(document), new StreamResult(sw));
  
          xmlstring = sw.toString();
          System.out.println(xmlstring);
          DOMSource domSource = new DOMSource(stmt);
  
          transformer.transform(domSource, streamResult);
          System.out.println("File saved to specified path!");
  
  
      }
  
      public static void main(String arg[]) {
          try {
  
              Createxmls xml = new Createxmls();
              xml.CreateXML();
          } catch (ParserConfigurationException ex) {
              Logger.getLogger(Createxmls.class.getName()).log(Level.SEVERE, null, ex);
          } catch (TransformerConfigurationException ex) {
              Logger.getLogger(Createxmls.class.getName()).log(Level.SEVERE, null, ex);
          } catch (TransformerException ex) {
              Logger.getLogger(Createxmls.class.getName()).log(Level.SEVERE, null, ex);
          }
  
      }
  }
  </code></pre>
  
  <p>When i displayed the contents of xml file for the query:
  "select max(id) from facedictionary where id = (select id from facedetails where id like 34 group by id))</p>
  
  <p>The result i got</p>
  
  <pre><code>   &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  -&lt;SQLStatement&gt;
  
  &lt;select&gt;max(id)&lt;/select&gt;
  
  &lt;from&gt;facedictionary&lt;/from&gt;
  
  &lt;where&gt;id = (&lt;/where&gt;
  
  &lt;select&gt;id&lt;/select&gt;
  
  &lt;from&gt;facedetails&lt;/from&gt;
  
  &lt;where&gt;id like 34 group by id)&lt;/where&gt;
  
  &lt;/SQLStatement&gt;
  </code></pre>
  
  <p>The result i need:</p>
  
  <pre><code>      &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  -&lt;SQLStatement&gt;
  
  &lt;select&gt;max(id)&lt;/select&gt;
  
  &lt;from&gt;facedictionary&lt;/from&gt;
  
  &lt;where&gt;id = (
  
          &lt;select&gt;id&lt;/select&gt;
  
          &lt;from&gt;facedetails&lt;/from&gt;
  
          &lt;where&gt;id like 34 group by id)&lt;/where&gt;
  &lt;/where&gt;
  
  &lt;/SQLStatement&gt;
  </code></pre>