Link to home
Start Free TrialLog in
Avatar of umesh4exp
umesh4expFlag for India

asked on

How to create a sql file using xml and java?

How to create a sql file using xml and java? code attached shows my xml and java and sample out put .sql file , but the problem is i am not able to achieve sample out put .sql file?
ReadTableXML.java.txt
Table.xml.txt
UMT.sql.txt
Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

i have corrected few problems in the code
Please check

import java.io.*;
import java.util.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;

public class ReadXMLTable {
	private static List myEmpls;
	private static Document dom;

	public ReadXMLTable() 
	{
		// create a list to hold the employee objects
		myEmpls = new ArrayList();
	}

	public static void main(String argv[]) {

		new ReadXMLTable(); 
		
		FileOutputStream out;
		PrintStream p;
		String textVal = null;

		try {
			File fXmlFile = new File("D:\\Table.xml");
			try {
				out = new FileOutputStream("D:\\UMT.sql");
				p = new PrintStream(out);

				DocumentBuilderFactory dbFactory = DocumentBuilderFactory
						.newInstance();
				DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
				Document doc = dBuilder.parse(fXmlFile);
				Element docEle = doc.getDocumentElement();
				docEle.normalize();
				System.out.println("Root element :"
						+ docEle.getNodeName());
				NodeList nList = doc.getElementsByTagName(docEle.getNodeName());
				System.out.println("-----------------------");

				p.println("-- GENERATED FILE : "
						+ docEle.getNodeName() + ".sql");

				for (int temp = 0; temp < nList.getLength(); temp++) {

					Node nNode = nList.item(temp);
					System.out.println("Before Enter IF");

					if (nNode.getNodeType() == Node.ELEMENT_NODE) {

						Element eElement = (Element) nNode;


						p.println("-- Table Description :"
								+ getTagValue("TABLE_DESC", eElement));
						p.println("");
						p.println("Drop table "
								+ docEle.getNodeName() + ";");
						p.println("");
						p.println("Drop synonym "
								+ getTagValue("TABLE_SYN", eElement) + ";");
						p.println("");
						p.println("Create Table "
								+ docEle.getNodeName()
								+ "   (");
						p.println("");

						NodeList fieldsElement = docEle.getElementsByTagName("FIELDS");
						Node nl = fieldsElement.item(0);
						
						NodeList childNodes = nl.getChildNodes();
											
						if (childNodes != null && childNodes.getLength() > 0) 
						{
							System.out.println("nl.getLength():" + childNodes.getLength());
							for (int i = 0; i < childNodes.getLength(); i++) 
							{
								
								Node el = childNodes.item(i);
								System.out.println(el);
								if (childNodes != null && childNodes.getLength() > 0) 
								{
									el = childNodes.item(0);
									System.out.println( el.getNodeValue() );
									
								}
								
							}
						}
						p.close();
					}
				}
			} 
			catch (Exception e) 
			{
				System.err.println("Error writing to file");
				e.printStackTrace();
			}
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}

	}

	private static String getTagValue(String sTag, Element eElement) {
		NodeList nlList = eElement.getElementsByTagName(sTag).item(0)
				.getChildNodes();
		Node nValue = (Node) nlList.item(0);

		return nValue.getNodeValue();

	}
}

Open in new window

Avatar of umesh4exp

ASKER

it is generating the .sql which is not complete, please check the files attached
complete-UMT.sql.txt
partial-UMT.sql.txt
ASKER CERTIFIED SOLUTION
Avatar of malaki12003
malaki12003
Flag of Iran, Islamic Republic of image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks