queria montar um xml com os dados que tenho num objeto. alguem tem um exemplo.
ps: nao posso usar nenhuma ferramenta que monta automatico, como xtream por exemplo…
Obrigado
queria montar um xml com os dados que tenho num objeto. alguem tem um exemplo.
ps: nao posso usar nenhuma ferramenta que monta automatico, como xtream por exemplo…
Obrigado
ta na mão…
import java.io.IOException;
import java.io.StringWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public class GeraXMLDOM {
/**
*
* @param deptno
* @param dname
* @param loc
* @return
*/
private Element getDeptElement(String deptno, String dname, String loc) {
Element elDept = new Element("departamento");
Attribute atDeptno = new Attribute("deptno", deptno);
elDept.setAttribute(atDeptno);
Element elDname = new Element("dname");
elDname.setText(dname);
elDept.addContent(elDname);
Element elLoc = new Element("loc");
elLoc.setText(loc);
elDept.addContent(elLoc);
return elDept;
}
/**
*
* @param doc
* @return
* @throws IOException
*/
private StringBuffer getDocumentAsString(Document doc) throws IOException {
// cria objeto XMLOPutputter para gerar o xml
XMLOutputter xmlOut = new XMLOutputter(Format.getPrettyFormat());
// cria objeto StringWriter
StringWriter sw = new StringWriter();
// gera saida do xml
xmlOut.output(doc, sw);
return sw.getBuffer();
}
/**
*
* @param name
* @return
*/
private Document getDocument(String name) {
// instancia document
Document doc = new Document();
// cria element raiz
Element elRoot = new Element(name);
//seta o elemento raiz
doc.setRootElement(elRoot);
return doc;
}
/**
* @param args
* @throws IOException
* @throws ClassNotFoundException
* @throws SQLException
*/
public static void main(String[] args) throws IOException,ClassNotFoundException, SQLException {
Connection conn = null;
ResultSet rs = null;
Statement stmt = null;
try {
// carrega driver
Class.forName("oracle.jdbc.OracleDriver");
String url = "SuaUrlDoBanco";
// abre conexao
conn = DriverManager.getConnection(url, "tecbmmab", "tecbmmab");
// cria statement
stmt = conn.createStatement();
String sql = "select * from dept";
// cria resultset com resultado da query
rs = stmt.executeQuery(sql);
GeraXMLDOM g = new GeraXMLDOM();
//instancia um document
Document doc = g.getDocument("departamento");
// percorrendo o ResultSet
while (rs.next()){
String deptno = rs.getString("deptno");
String dname = rs.getString("dname");
String loc = rs.getString("loc");
//cria um novo elemento
Element el = g.getDeptElement(deptno,dname,loc);
//adiciona ao elemento raiz
doc.getRootElement().addContent(el);
}
System.out.println(g.getDocumentAsString(doc).toString());
} finally {
if (rs != null){
rs.close();
}
if (stmt != null){
stmt.close();
}
if (conn != null) {
conn.close();
}
}
}
}
Salvou… Brigadao!!!
aqui… da erros nesses imports
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
qual jar eu preciso incluir?
ja achei… jdom.jar. obrigado