import java.io.*; import java.sql.*; import javax.xml.parsers.*; import javax.xml.transform.*; import javax.xml.transform.dom.*; import javax.xml.transform.stream.*; import org.w3c.dom.*; public class AineXmlExp{ //nimede puu static void nimed(Document d, Element njuur){ try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection cn=DriverManager.getConnection("jdbc:odbc:baas3", "", ""); Statement st=cn.createStatement(); ResultSet nimi=st.executeQuery("SELECT nimi, id_n FROM nimi"); while(nimi.next()){ Element n=d.createElement("nimi"); njuur.appendChild(n); Element nid=d.createElement("nime_id"); nid.appendChild(d.createTextNode(nimi.getString("id_n"))); n.appendChild(nid); Element sona=d.createElement("sona"); sona.appendChild(d.createTextNode(nimi.getString("nimi"))); n.appendChild(sona); } } catch(Exception e) { System.out.println("Miskit viga: "+e.getMessage()); e.printStackTrace(); } } //ainete puu static void ained(Document d, Element ajuur){ try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection cn=DriverManager.getConnection("jdbc:odbc:baas3", "", ""); Statement st=cn.createStatement(); ResultSet aine=st.executeQuery("SELECT * FROM aine"); while(aine.next()){ Element a=d.createElement("aine"); ajuur.appendChild(a); Element aid=d.createElement("aine_id"); aid.appendChild(d.createTextNode(aine.getString("id_a"))); a.appendChild(aid); Element sona1=d.createElement("sona1"); sona1.appendChild(d.createTextNode(aine.getString("aine"))); a.appendChild(sona1); } } catch(Exception e) { System.out.println("Miskit viga: "+e.getMessage()); e.printStackTrace(); } } public static void main(String argumendid[]) throws Exception{ Document d=DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); Element juur=d.createElement("kogu"); Element njuur=d.createElement("nimi"); Element ajuur=d.createElement("aine"); d.appendChild(juur); juur.appendChild(njuur); juur.appendChild(ajuur); nimed(d, njuur); ained(d, ajuur); Transformer t=TransformerFactory.newInstance().newTransformer(); FileOutputStream valja=new FileOutputStream("nimistu.xml"); t.transform(new DOMSource(d), new StreamResult(valja)); valja.close(); } }