Thursday, 16 June 2011

Retrieve data from database and create xml

example.java



package com.client;

import com.google.gwt.core.client.EntryPoint;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.Response;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.core.client.GWT;

public class example implements EntryPoint {

public void onModuleLoad()
{
RootPanel rootPanel = RootPanel.get();
String url =GWT.getModuleBaseURL()+"upload";
doPost(url);
}


public static void doPost(String url)

{

RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);


try

{

Request response = builder.sendRequest(null, new RequestCallback()

{

public void onError(Request request, Throwable exception)

{

System.out.print("error");
}

public void onResponseReceived(Request request,Response response)

{

//if (response.getStatusCode() == 200)
//{
Window.alert(response.getText()); // This gets the response message .
///}
//else

//{

// Window.alert("error");

//}
}

});
}
catch(Exception e)
{
System.out.print(e.getMessage());
}
}
}

XMLServlet.java



package com.server;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import org.w3c.dom.*;

import com.gwtext.client.data.XmlReader;

import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;

public class XmlServlet extends HttpServlet
{

NodeList list;
Connection con=null;
Statement st=null;
ResultSet rs = null;
String xmlString ;
BufferedWriter bw;
String getdataname;
String getdataaddress;
Document doc1;
  public void doGet(HttpServletRequest request,HttpServletResponse response)
   throws ServletException,IOException{



  response.setContentType("text/html");
  PrintWriter pw = response.getWriter();
  System.out.print("on server");
  try
  {


 DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
 DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
 //creating a new instance of a DOM to build a DOM tree.
 doc1 = docBuilder.newDocument();
 new XmlServlet().createXmlTree(doc1);



  }
  catch(Exception e)
  {
  System.out.println(e.toString());
  }

   }

  public void createXmlTree(Document doc) throws Exception {
  //This method creates an element node

 System.out.println("ruchipaliwal111");

 try
 {

 System.out.println("ruchi111");
 Class.forName("com.mysql.jdbc.Driver");
 con = DriverManager.getConnection("jdbc:mysql://localhost:3306/plz","root","enggheads");
 st = con.createStatement();

 rs = st.executeQuery("select * from data");


 Element root = doc.createElement("data");
 doc.appendChild(root);

   while(rs.next())
   {
  getdataname=rs.getString(1).toString();
   System.out.println(  getdataname +"getdataname");
   getdataaddress=rs.getString(2).toString();
   System.out.println(   getdataaddress +"  getdataaddress");
 
 
 
   //adding a node after the last child node of ssthe specified node.
 
   Element child1 = doc.createElement("name");
   root.appendChild(child1);
   Text text = doc.createTextNode(getdataname);
   child1.appendChild(text);
   Element element = doc.createElement("address");
   root.appendChild(element);
   Text text1 = doc.createTextNode( getdataaddress);
   element.appendChild(text1);

}


 


 
//TransformerFactory instance is used to create Transformer objects.
  TransformerFactory factory = TransformerFactory.newInstance();
  Transformer transformer = factory.newTransformer();

  transformer.setOutputProperty(OutputKeys.INDENT, "yes");
  transformer.setOutputProperty(OutputKeys.METHOD,"xml");
  // transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "3");
 
 
  // create string from xml tree
  StringWriter sw = new StringWriter();
  StreamResult result = new StreamResult(sw);
  DOMSource source = new DOMSource(doc);
  transformer.transform(source, result);

  xmlString = sw.toString();


  File file = new File("c:/newxml.xml");
  bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
  bw.write(xmlString);
 
  System.out.println("successfully done.....");

 }
 catch(Exception e)
 {
 System.out.print("after while loop exception"+e.toString());
 }
 
  bw.flush();
  bw.close();
  System.out.println("successfully done.....");
  }
}



newxml.xml

<?xml version="1.0" encoding="UTF-8"?><data>
<name>ruchi</name>
<address>fgdskgh</address>
<name>ruchi12</name>
<address>fgdskghvg</address>
</data>

No comments:

Post a Comment