oki to wiem juz zauwazylem ;) dzieki.
Teraz jest inny problem w ogóle to chodzi o to by stworzyl xml ale umial dodawac nowe elementy czyli tworzyl tak:
<?xml version="1.0" encoding="Windows-1250"?>
<zgloszenie>
<dane=1>
<nr>ff</nr>
<data>ff</data>
<imie>f</imie>
<nazwisko>fff</nazwisko>
<sprzet />
<opis />
<status />
</dane>
<dane=2>
<nr>fffh</nr>
<data>rrrrrrr</data>
<imie>rrrr</imie>
<nazwisko>rr</nazwisko>
<sprzet />
<opis />
<status />
</dane>
</zgloszenie>
a nie :
<?xml version="1.0" encoding="Windows-1250"?>
<zgloszenie>
<dane>
<nr>ff</nr>
<data>ff</data>
<imie>f</imie>
<nazwisko>fff</nazwisko>
<sprzet />
<opis />
<status />
</dane>
</zgloszenie>
<?xml version="1.0" encoding="Windows-1250"?>
<zgloszenie>
<dane>
<nr>fffh</nr>
<data>rrrrrrr</data>
<imie>rrrr</imie>
<nazwisko>rr</nazwisko>
<sprzet />
<opis />
<status />
</dane>
</zgloszenie>
O to kod:
package pl.toXML.app;
import nu.xom.*;
import java.io.*;
import java.util.*;
public class Zgloszenia {
public String nr, data, imie, nazwisko, sprzet, opis, status;
public static String xml;
public Zgloszenia(String nr, String data, String imie, String nazwisko, String sprzet, String opis, String status){
this.nr = nr;
this.data = data;
this.imie = imie;
this.nazwisko = nazwisko;
this.sprzet = sprzet;
this.opis = opis;
this.status = status;
this.xml = xml;
}
//Tworzy element (Element) XML na bazie obiektu Zgloszenie (this):
public Element getXML(){
Element zgloszenie = new Element("zgloszenie");
Element nrid = new Element("nr");
nrid.appendChild(nr);
Element dataid = new Element("data");
dataid.appendChild(data);
Element imieid = new Element("imie");
imieid.appendChild(imie);
Element nazwiskoid = new Element("nazwisko");
nazwiskoid.appendChild(nazwisko);
Element sprzetid = new Element("sprzet");
sprzetid.appendChild(sprzet);
Element opisid = new Element("opis");
opisid.appendChild(opis);
Element statusid = new Element("status");
statusid.appendChild(status);
zgloszenie.appendChild(nrid);
zgloszenie.appendChild(dataid);
zgloszenie.appendChild(imieid);
zgloszenie.appendChild(nazwiskoid);
zgloszenie.appendChild(sprzetid);
zgloszenie.appendChild(opisid);
zgloszenie.appendChild(statusid);
return zgloszenie;
}
//Konstruktor przywracający obiekt Zgloszenie z elementu XML:
public Zgloszenia(Element zgloszenie){
nr = zgloszenie.getFirstChildElement("nr").getValue();
data = zgloszenie.getFirstChildElement("data").getValue();
imie = zgloszenie.getFirstChildElement("imie").getValue();
nazwisko = zgloszenie.getFirstChildElement("nazwisko").getValue();
sprzet = zgloszenie.getFirstChildElement("sprzet").getValue();
opis = zgloszenie.getFirstChildElement("opis").getValue();
status = zgloszenie.getFirstChildElement("status").getValue();
}
public String toString(){return nr + " " + data;}
//Dla czytelności:
public static void format(OutputStream os, Document doc) throws Exception{
Serializer serializer = new Serializer(os,"Windows-1250");
serializer.setIndent(4);
serializer.setMaxLength(60);
serializer.write(doc);
serializer.flush();
}
public static void dodajrekord () throws Exception {
String pierwsza = app.Zgloszenie.jTextField2.getText();
String druga = app.Zgloszenie.jTextField3.getText();
String trzecia = app.Zgloszenie.jTextField4.getText();
String czwarta = app.Zgloszenie.jTextArea1.getText();
List<Zgloszenia> zglos = Arrays.asList(new Zgloszenia("" + pierwsza, "" + druga, "" + trzecia, "" + czwarta, "", "", ""));
Element root = new Element("zgloszenie");
for(int i = 0; i < zglos.size(); i++) {
root.appendChild(zglos.get(i).getXML());
new Object(); // tworzenie nowego obiektu
}
}
public static void glowny() throws Exception{
Document doc = new Document(root);
format(System.out, doc);
format(new BufferedOutputStream(new FileOutputStream(
"zgloszenie.xml")), doc);
}
}
dodaj rekord w zamysle bedzie wywolywany w buttonie dodaj i bedzie dodawac rekordy
a glowny w zapisz i bedzie zapisywac do xml
Nie wiem czy w ogóle dobrze mysle