import java.io.FileWriter; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import oracle.jdbc.rowset.OracleCachedRowSet; import org.w3c.dom.Document; import org.w3c.dom.Element; public class JobsTableToXML { public static void main(String[] args) throws Exception { // connect to oracle and retrieve data OracleCachedRowSet crs = new OracleCachedRowSet(); crs.setUrl("jdbc:oracle:thin:@localhost:1521:xe"); crs.setUsername("hr"); crs.setPassword("hr"); crs.setCommand("select job_id, job_title from jobs"); crs.execute(); // create xml document from scratch DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.newDocument(); Element jobs, job, ele; jobs = (Element) document.createElement("jobs"); document.appendChild(jobs); while (crs.next()) { // write one job at a time to xml document job = (Element) document.createElement("job"); ele = document.createElement("id"); ele.appendChild(document.createTextNode(crs.getString("job_id"))); job.appendChild(ele); ele = document.createElement("title"); ele.appendChild(document.createTextNode(crs.getString("job_title"))); job.appendChild(ele); jobs.appendChild(job); } // Write XML document from memory to jobs.xml FileWriter fw = new FileWriter("c:\\jobs.xml"); TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); DOMSource source = new DOMSource(document); StreamResult result = new StreamResult(fw); transformer.transform(source, result); } }
import java.io.FileWriter; import oracle.jdbc.rowset.OracleWebRowSet; public class RowSetToXML { public static void main(String args[]) throws Exception { // get data from JOBS table OracleWebRowSet wrs = new OracleWebRowSet(); wrs.setUrl("jdbc:oracle:thin:@localhost:1521:xe"); wrs.setUsername("hr"); wrs.setPassword("hr"); wrs.setCommand("select job_id, job_title from jobs"); wrs.execute(); FileWriter fw = new FileWriter("c:\\jobs.xml"); wrs.writeXml(fw); } }