Advertisement
albertoaguilar8

Untitled

Sep 19th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.28 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package xmlparsingexample;
  7.  
  8. import java.io.File;
  9. import javax.xml.parsers.*;
  10. import javax.xml.transform.*;
  11. import javax.xml.transform.dom.*;
  12. import javax.xml.transform.stream.*;
  13. import org.w3c.dom.*;
  14. import java.sql.*;
  15.  
  16. /**
  17.  *
  18.  * @author alberto
  19.  */
  20. public class XMLParsingExample {
  21.  
  22.     /**
  23.      * @param args the command line arguments
  24.      */
  25.     public static void main(String[] args) {
  26.         // TODO code application logic here
  27.         try {
  28.             Connection conn;
  29.            
  30.             DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
  31.             DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
  32.            
  33.             // root element
  34.             Document doc = docBuilder.newDocument();
  35.             Element rootElement = doc.createElement("wad_course");
  36.             doc.appendChild( rootElement );
  37.  
  38.             String s =
  39.                 "jdbc:mysql://cml.chi.itesm.mx/wad?user=wad&password=p5zVDmq4IGto";
  40.             conn = DriverManager.getConnection( s );
  41.  
  42.             Statement stmt = conn.createStatement();
  43.             ResultSet rs = stmt.executeQuery("select * from student");
  44.             while ( rs.next() ) {
  45.                 String i = rs.getString("id");
  46.                 String first = rs.getString("firstname");
  47.                 String last = rs.getString("lastname");
  48.                 String nick = rs.getString("nickname");
  49.                 String g = rs.getString("grade");
  50.                
  51.                 // student element
  52.                 Element student = doc.createElement("student");
  53.                 student.setAttribute("id", i);
  54.  
  55.                 // add student to root
  56.                 rootElement.appendChild( student );
  57.  
  58.                 // first name
  59.                 Element firstname = doc.createElement("firstname");
  60.                 firstname.appendChild( doc.createTextNode(first) );
  61.                 student.appendChild( firstname );
  62.  
  63.                 // last name
  64.                 Element lastname = doc.createElement("lastname");
  65.                 lastname.appendChild( doc.createTextNode( last) );
  66.                 student.appendChild( lastname );
  67.  
  68.                 // nickname
  69.                 Element nickname = doc.createElement("nickname");
  70.                 nickname.appendChild( doc.createTextNode(nick) );
  71.                 student.appendChild( nickname );
  72.  
  73.                 // grade
  74.                 Element grade = doc.createElement("grade");
  75.                 grade.appendChild( doc.createTextNode(g));
  76.                 student.appendChild( grade );
  77.             }
  78.            
  79.            
  80.             // write to XML file
  81.             TransformerFactory transformerFactory = TransformerFactory.newInstance();
  82.             Transformer transformer = transformerFactory.newTransformer();
  83.             DOMSource source = new DOMSource( doc );
  84.             StreamResult result = new StreamResult( new File("/Users/alberto/testing.xml"));
  85.             transformer.transform( source, result);
  86.            
  87.         } catch (Exception e) {
  88.             System.out.println(e.getLocalizedMessage());
  89.         }
  90.     }
  91.    
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement