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
 

marino51

Utente Attivo
28 Feb 2013
3.203
207
63
Lombardia
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());
 }
 }
 

tigre2209

Utente Attivo
19 Set 2007
57
7
8
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());
}
 

tigre2209

Utente Attivo
19 Set 2007
57
7
8
ME li ha messi Eclipse, quando ho cliccato su un errore.
comunque grazie per la risposta, se sai il numero di telefono di eclipse
 

marino51

Utente Attivo
28 Feb 2013
3.203
207
63
Lombardia
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
Autore Titolo Forum Risposte Data
L [php e xml] per creare una galleria fotografica con diversi file xml PHP 0
F come creare un search box coi file .xml dei merchant? XML 0
M Creare traccia di download file sul server HTML e CSS 2
U PHP creare un file excel dopo ricerca nel DB PHP 0
S Libreria PHP per creare file dwg o dxf PHP 0
W creare file .jar Java 2
Federico.Marcelo qual è il modo più semplice per creare file GIF di alta qualità? Webdesign e Grafica 4
andreas88 Creare file .htaccess per risolvere alcuni problemi con il tester SEO SEO e Posizionamento 0
B funzioni per creare file e inserirli dentro una certa cartella in php? PHP 1
SebaGravi Creare file per ogni news presente nel DB SEO e Posizionamento 2
giannit [PHP] Creare pagina con collegamenti ipertestuali da file txt PHP 6
L [PHP] Creare un uploader di file csv xls e ricercare i risultati PHP 0
romeocharly E' possibile creare un collegamento tra un id di db mysql ed un file per il download? PHP 0
romeocharly Creare un script/codice in php per collegare un id mysql con un file PHP 0
Monital Creare file json PHP 2
Monital Creare un file PHP con PHP PHP 13
T creare una serie di combobox e text box e che salvano un file di text sul disco si può ? Visual Basic 1
P come creare un file asp Classic ASP 1
I Creare file immagine da testo base64 PHP 2
C File html con procedure javascript per creare effetto libro da sfogliare Javascript 9
L Creare proprietario, gruppi e utenti per permessi cartella e file PHP 4
J Creare un file .xls PHP 1
J Creare file html da un form in area riservata PHP 9
O Software per creare una galleria di file .SWF Flash 1
M creare file .txt, salvarci informazioni e rileggere il contenuto Java 1
F Creare un file in PDF Windows e Software 4
G Aiuto per creare un form di invio file Classic ASP 3
Z Creare File xls con le classi PHP 0
A creare file php PHP 1
A Creare un file Programmazione 2
V AIUTO: creare file php x form PHP 2
R Creare un file di Microsoft Word con ASP : Testo Ok. E le immagini? Classic ASP 1
M Creare link a file dinamicamente Classic ASP 1
L Creare File Pdf PHP 2
B Che file devo creare???? Classic ASP 1
I creare file txt PHP 3
E Creare un testo trasparente dietro un div HTML e CSS 5
A Cerco programmatore/sviluppatore per creare forum Offerte e Richieste di Lavoro e/o Collaborazione 2
L Creare ancore interne alla pagina PHP 1
I Creare un banner temporaneo JavaScript Javascript 0
C Creare Tabella Dati Meteo prelevandoli da dei Tags in un altro foglio PHP 27
simgia Cordova errore quando cerco di emulare o creare la app Sviluppo app per Android 2
A Creare un cronometro PHP 5
G [PHP] Creare script di prenotazione con controllo disponibilità. PHP 7
F Creare elementi html con javascript Javascript 3
Shyson Modificare codice e creare link PHP 0
Cosina Creare bottone delete in form upload PHP 5
Cosina Creare bottone delete in form upload PHP 1
M Creare un campo input select quantità di un numero intero prelevato dal db PHP 3
L Creare una forma geometrica Photoshop 1

Discussioni simili