Ho un codice che crea un file xml.
questo;
la modifica è questa:
questo;
ho voluto modificarlo perche i dati che devo creare li rpendo da un database.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());
}
}
la modifica è questa:
il problema è che mi scrive solo uL'ULTIMO record e non tutti probabilmente non aggiunge le righetry
{
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!");