在xml文件中存储sql查询

时间:2014-12-01 06:43:45

标签: java sql xml xml-parsing

我想在xml文件中以树格式存储sql查询。我编写了一个代码,将代码分成不同的标记并将其存储在xml文件中。我存储的关键字如" select",from,其中etc作为xmlelements以及这些关键字的值作为相应元素中的textnodes。当查询包含单个单个语句时,它可以正常工作。但是当嵌套查询发生时,我想将嵌套查询存储在"其中"在这种情况下,代码没有正常工作。我特此附上代码。 任何人都可以指出我的代码中的逻辑错误????

第二个问题是当我尝试将xmldata存储在" CreateFiles.xml"文件,文件没有显示在相应的文件夹中。原因是什么?

CreateXml.java

    /*
 * 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 < 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);
        }

    }
}

当我显示查询的xml文件的内容时: &#34;从facesictionary中选择max(id),其中id =(从facedetails中选择id,其中id为34 group by id))

我得到的结果

   <?xml version="1.0" encoding="UTF-8"?>

-<SQLStatement>

<select>max(id)</select>

<from>facedictionary</from>

<where>id = (</where>

<select>id</select>

<from>facedetails</from>

<where>id like 34 group by id)</where>

</SQLStatement>

我需要的结果:

      <?xml version="1.0" encoding="UTF-8"?>

-<SQLStatement>

<select>max(id)</select>

<from>facedictionary</from>

<where>id = (

        <select>id</select>

        <from>facedetails</from>

        <where>id like 34 group by id)</where>
</where>

</SQLStatement>

0 个答案:

没有答案