import javax.xml.parsers.*; import javax.xml.transform.*; import javax.xml.transform.dom.*; import javax.xml.transform.stream.*; import java.io.*; import java.net.*; import java.sql.*; import java.util.*; import org.w3c.dom.*; public class UusDokument2{ public static void main(String argumendid[]) throws Exception{ PrintWriter valja=new PrintWriter(new FileWriter("news.xml")); try { Document d=DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); Element juur=d.createElement("portal"); d.appendChild(juur); // // Andmebaasi ühendamine // Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection cn=DriverManager.getConnection( "jdbc:odbc:andmebaas", "", ""); Statement st=cn.createStatement(); Connection co=DriverManager.getConnection( "jdbc:odbc:andmebaas", "", ""); Statement kahst=co.createStatement(); String lause="SELECT * FROM news_entries order by id"; ResultSet rs=st.executeQuery(lause); String kahlause; ResultSet kahrs; while(rs.next()) { Element e=d.createElement("news"); // Pealkiri Element f=d.createElement("title"); f.appendChild(d.createTextNode(rs.getString("title"))); e.appendChild(f); // Teema kahlause="SELECT * FROM news_topics where id = '"+rs.getString("topic_id")+"'"; kahrs=kahst.executeQuery(kahlause); while(kahrs.next()) { Element g=d.createElement("topic"); g.appendChild(d.createTextNode(kahrs.getString("topic"))); e.appendChild(g); } // Uudis Element h=d.createElement("content"); h.appendChild(d.createTextNode(rs.getString("news"))); e.appendChild(h); kahlause="SELECT * FROM news_comments where entry_id = '"+rs.getString("id")+"' order by id"; kahrs=kahst.executeQuery(kahlause); Element i=d.createElement("comments"); while(kahrs.next()) { // Kommentaarid // Kommentaar Element j=d.createElement("comment"); j.appendChild(d.createTextNode(kahrs.getString("comment"))); i.appendChild(j); // Email Element k=d.createElement("email"); k.appendChild(d.createTextNode(kahrs.getString("email"))); i.appendChild(k); } e.appendChild(i); juur.appendChild(e); } Transformer t=TransformerFactory.newInstance().newTransformer(); t.transform(new DOMSource(d), new StreamResult(valja)); valja.close(); } catch(Exception viga) { viga.printStackTrace(); } } }