creare file xml

tigre2209

Utente Attivo
19 Set 2007
57
7
8
Ho un codice che crea un file xml.
questo;
try {

Element company = new Element("company");
Document doc = new Document(company);
doc.setRootElement(company);

Element staff = new Element("staff");
staff.setAttribute(new Attribute("id", "1"));
staff.addContent(new Element("firstname").setText("yong"));
staff.addContent(new Element("lastname").setText("mook kim"));
staff.addContent(new Element("nickname").setText("mkyong"));
staff.addContent(new Element("salary").setText("199999"));
doc.getRootElement().addContent(staff);

Element staff2 = new Element("staff");
staff2.setAttribute(new Attribute("id", "2"));
staff2.addContent(new Element("firstname").setText("low"));
staff2.addContent(new Element("lastname").setText("yin fong"));
staff2.addContent(new Element("nickname").setText("fong fong"));
staff2.addContent(new Element("salary").setText("188888"));

doc.getRootElement().addContent(staff2);

// new XMLOutputter().output(doc, System.out);
XMLOutputter xmlOutput = new XMLOutputter();

// display nice nice
xmlOutput.setFormat(Format.getPrettyFormat());
xmlOutput.output(doc, new FileWriter("c:\\file.xml"));

System.out.println("File Saved!");
} catch (IOException io)
{
System.out.println(io.getMessage());
}
}
ho voluto modificarlo perche i dati che devo creare li rpendo da un database.
la modifica è questa:
try
{
while (rs.next())
{
Element spettanze = new Element("spettanze");
Document doc = new Document(spettanze);
doc.setRootElement(spettanze);
Element staff = new Element("staff");
staff.setAttribute(new Attribute("cod", rs.getString(1)));
staff.addContent(new Element("codice").setText("COD"));
staff.addContent(new Element("valore").setText(rs.getString(2)));
doc.getRootElement().addContent(staff);
}
XMLOutputter xmlOutput = new XMLOutputter();
xmlOutput.setFormat(Format.getPrettyFormat());
xmlOutput.output(doc, new FileWriter(filename));

System.out.println("File Saved!");
il problema è che mi scrive solo uL'ULTIMO record e non tutti probabilmente non aggiunge le righe
 
non ho esperienza specifica ma cercherei di rispettare l'ordine del primo esempio,
lasciando a te di cambiare i nomi per la tua nuova esigenza
ciao
Marino
Codice:
try {

 Element company = new Element("company");
 Document doc = new Document(company);
 doc.setRootElement(company);

while (rs.next())
 {
 Element staff = new Element("staff");
 staff.setAttribute(new Attribute("id", "1"));
 staff.addContent(new Element("firstname").setText("yong"));
 staff.addContent(new Element("lastname").setText("mook kim"));
 staff.addContent(new Element("nickname").setText("mkyong"));
 staff.addContent(new Element("salary").setText("199999"));
 doc.getRootElement().addContent(staff);
 }

 // new XMLOutputter().output(doc, System.out);
 XMLOutputter xmlOutput = new XMLOutputter();

 // display nice nice
 xmlOutput.setFormat(Format.getPrettyFormat());
 xmlOutput.output(doc, new FileWriter("c:\\file.xml"));

 System.out.println("File Saved!");
 } catch (IOException io)
 {
 System.out.println(io.getMessage());
 }
 }
 
Scusa, controordine
ho provato cosi come mi hai consigliato:
try {

Element company = new Element("company");
Document doc = new Document(company);
doc.setRootElement(company);


while (rs.next())
{
Element staff = new Element("COD");
staff.setAttribute(new Attribute("id",rs.getString(1)));
staff.addContent(new Element("CODICE").setText("IBAN"));
staff.addContent(new Element("VALORE").setText(rs.getString(2)));

doc.getRootElement().addContent(staff);
a++;
}



// new XMLOutputter().output(doc, System.out);
XMLOutputter xmlOutput = new XMLOutputter();

// display nice nice
xmlOutput.setFormat(Format.getPrettyFormat());
xmlOutput.output(doc, new FileWriter(filename));

System.out.println("File Saved!");
}
catch (IOException io)
{
System.out.println(io.getMessage());
}
e mi da errore su rs.next
Unhandled exception type SQLException
invece cosi va, ma non capisco perche mi ha messo due try
try {

Element company = new Element("company");
Document doc = new Document(company);
doc.setRootElement(company);

try {
while (rs.next())
{
Element staff = new Element("ID");
staff.setAttribute(new Attribute("id",rs.getString(1)));
staff.addContent(new Element("CODICE").setText("ISPFE"));
staff.addContent(new Element("VALORE").setText(rs.getString(2)));
// staff.addContent(new Element("nickname").setText("mkyong"));
// staff.addContent(new Element("salary").setText("199999"));
doc.getRootElement().addContent(staff);
a++;
}

}
catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

// new XMLOutputter().output(doc, System.out);
XMLOutputter xmlOutput = new XMLOutputter();

// display nice nice
xmlOutput.setFormat(Format.getPrettyFormat());
xmlOutput.output(doc, new FileWriter(filename));

System.out.println("File Saved!");
} catch (IOException io)
{
System.out.println(io.getMessage());
}
 
ME li ha messi Eclipse, quando ho cliccato su un errore.
comunque grazie per la risposta, se sai il numero di telefono di eclipse
 
il try-catch più interno gestisce la fine del cursore "rs.next" non considerandolo errore
mentre lo stesso evento gestito dal try-catch più esterno non lo identifica come fine del cursore segnalando l'errore

per quanto riguarda il telefono non ho nessun contatto ma se per eclipse intendi lo stesso software che conosco io,
puoi in Google inserire "eclipse download" e vieni mandato sul sito dove trovi la pagina contatti
ciao
Marino
 

Discussioni simili