Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package sw;
- import java.sql.*;
- import java.io.StringWriter;
- import java.io.Writer;
- import java.util.ArrayList;
- import java.util.Iterator;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import javax.xml.transform.OutputKeys;
- import javax.xml.transform.Transformer;
- import javax.xml.transform.TransformerFactory;
- import javax.xml.transform.dom.DOMSource;
- import javax.xml.transform.stream.StreamResult;
- import org.w3c.dom.Comment;
- import org.w3c.dom.Document;
- import org.w3c.dom.Element;
- /**
- *
- * @author student
- */
- public class SW{
- /**
- * @param args the command line arguments
- */
- //public static void main(String[] args) throws Exception {
- //
- //
- //
- //}
- public static void main(String[] args) throws ClassNotFoundException, SQLException, Exception {
- String url="jdbc:derby://localhost:1527/tripss";
- final String JDBC_DRIVER = "com.derby.jdbc.Driver";
- final String DB_URL = "jdbc:derby://localhost/tripss";
- Connection con=DriverManager.getConnection(url, "a", "a");
- Statement instr=con.createStatement();
- Statement instr2=con.createStatement();
- String sql="SELECT * FROM a.trips t";
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
- DocumentBuilder builder = dbf.newDocumentBuilder();
- Document doc = builder.newDocument();
- Element element = doc.createElement("trips");
- doc.appendChild(element);
- //Comment comment = doc.createComment("This is a comment");
- //doc.insertBefore(comment, element);
- //element.appendChild(itemElement);
- //itemElement.setAttribute("myattr", "attrvalue");
- //itemElement.insertBefore(doc.createTextNode("text"), itemElement.getLastChild());
- ArrayList<Trip> trips = new ArrayList<Trip>();
- //String sql2="SELECT t.Title FROM app.trip t, app.address a where p.address= a.id and a.city= 'Bucharest'";
- ResultSet rs=instr.executeQuery(sql);
- //ResultSet rs2=instr2.executeQuery(sql2);
- while(rs.next()){
- Trip t = new Trip(Integer.parseInt(rs.getString(1)),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5), rs.getString(6));
- trips.add(t);
- }
- Iterator<Trip> it= trips.iterator();
- for (Trip t:trips) {
- Element itemElement = doc.createElement("trip");
- element.appendChild(itemElement);
- Element agencyElement = doc.createElement("agency");
- itemElement.appendChild(agencyElement);
- Element titleElement = doc.createElement("title");
- itemElement.appendChild(titleElement);
- Element descriptionElement = doc.createElement("description");
- itemElement.appendChild(descriptionElement);
- itemElement.setAttribute("id", ""+t.getId());
- itemElement.setAttribute("startDate", ""+t.getStartt());
- itemElement.setAttribute("endDate", ""+t.getEndt());
- itemElement.insertBefore(doc.createTextNode(""), itemElement.getLastChild());
- agencyElement.insertBefore(doc.createTextNode(t.getAgency()), agencyElement.getLastChild());
- titleElement.insertBefore(doc.createTextNode(t.getTitle()), titleElement.getLastChild());
- descriptionElement.insertBefore(doc.createTextNode(t.getDescription()), descriptionElement.getLastChild());
- }
- // while(it.hasNext()) {
- // itemElement.setAttribute("myattr", Integer.toString(it.next().getId()));
- // itemElement.setAttribute("myattr", it.next().getStartt());
- // itemElement.setAttribute("myattr", it.next().getEndt());
- // itemElement.insertBefore(doc.createTextNode("Agency"), itemElement.getLastChild());
- // }
- // while(rs2.next()){
- // System.out.println(rs2.getString("firstName"));
- // }
- prettyPrint(doc);
- rs.close();
- instr.close();
- con.close();
- //rs2.close();
- instr2.close();
- con.close();
- }
- public static final void prettyPrint(Document xml) throws Exception {
- Transformer tf = TransformerFactory.newInstance().newTransformer();
- tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
- tf.setOutputProperty(OutputKeys.INDENT, "yes");
- Writer out = new StringWriter();
- tf.transform(new DOMSource(xml), new StreamResult(out));
- System.out.println(out.toString());
- }
- }
- package sw;
- /**
- *
- * @author student
- */
- public class Trip {
- private int id;
- private String endt;
- private String startt;
- private String Agency;
- public int getId() {
- return id;
- }
- public String getEndt() {
- return endt;
- }
- public String getStartt() {
- return startt;
- }
- public String getAgency() {
- return Agency;
- }
- public String getTitle() {
- return title;
- }
- public String getDescription() {
- return Description;
- }
- private String title;
- private String Description;
- public Trip(int id, String endt, String startt, String Agency, String title, String Description) {
- this.id = id;
- this.endt = endt;
- this.startt = startt;
- this.Agency = Agency;
- this.title = title;
- this.Description = Description;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement