Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class xmltosql extends HttpServlet {
- @Override
- public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException
- {
- System.out.println("this is in current data");
- try
- {
- // instream receives string from server 1
- InputStream in = req.getInputStream();
- // testing code
- DocumentBuilderFactory factory = null;
- DocumentBuilder builder = null;
- Document ret = null;
- try {
- factory = DocumentBuilderFactory.newInstance();
- builder = factory.newDocumentBuilder();
- } catch (ParserConfigurationException e) {
- e.printStackTrace();
- }
- try {
- // creating xml document ret using instream data
- ret = builder.parse(in);
- System.out.println("ret : "+ret);
- } catch (SAXException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- try
- {
- // establishing jdbc connection to database on server 2
- Class.forName("com.mysql.jdbc.Driver");
- Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/nic", "root", "jumanjis");
- Statement st = con.createStatement();
- // initiating a queue to recieve object
- Queue<data> q = new LinkedList<data>();
- if(req!=null){
- // initiating object of data class which takes row values of table on server 1
- data obj = new data();
- // reading xml string from inputstream
- NodeList employeelist = ret.getElementsByTagName("employee");
- for(int i=0; i<employeelist.getLength();i++){
- Node n = employeelist.item(i);
- if(n.getNodeType()==Node.ELEMENT_NODE){
- Element employee = (Element) n;
- NodeList childnodes = employee.getChildNodes();
- for(int j=0;j<childnodes.getLength();j++){
- Node child = childnodes.item(j);
- if(child.getNodeType()==Node.ELEMENT_NODE){
- Element x = (Element) child;
- // initializing members of data class object
- switch (x.getTagName()) {
- case "id":
- obj.id = Integer.parseInt(x.getTextContent());
- break;
- case "name":
- obj.name = x.getTextContent();
- break;
- case "age":
- obj.age = Integer.parseInt(x.getTextContent());
- break;
- case "city":
- obj.city = x.getTextContent();
- break;
- case "country":
- obj.country = x.getTextContent();
- break;
- default:
- break;
- }
- }
- }
- }
- }
- // inserting object of data class to queue
- q.add(obj);
- // iterating through the objects in the queue
- Iterator iterator = q.iterator();
- while(iterator.hasNext()){
- data first =q.remove();
- // updating the table xyz on server 2 using the object in queue
- st.executeUpdate("insert into xyz values("+first.id+", '"+first.name+"', "+first.age+", '"+first.city+"', '"+first.country+"')");
- }
- ret = null;
- }
- }
- catch(Exception ex)
- {
- ex.printStackTrace();
- }
- // storing data in db
- }
- catch (IOException e)
- {
- System.err.println(e);
- e.printStackTrace();
- }
- }
- }
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
- version="3.1">
- <servlet>
- <servlet-name>xmltosql</servlet-name>
- <servlet-class>xmltosql</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>xmltosql</servlet-name>
- <url-pattern></url-pattern>
- </servlet-mapping>
- </web-app>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement