package xmldemo;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Iterator;
import java.util.Vector;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.Text;
public class XMLOutDemo
{
private String filename;
private Vector v_data;
private Document doc;
public XMLOutDemo()
{
this.v_data = this.get_dummy_data();
this.filename = "/home/steffen/xmlout.xml";
this.doc = this.get_new_document();
this.build_document();
this.save_document();
System.out.println("fertig");
}
private void build_document()
{
Node no_adrverw = this.doc.createElement("adressenverwaltung");
Iterator it = this.v_data.iterator();
while (it.hasNext())
{
Mitarbeiter mit = (Mitarbeiter)it.next();
Node no_adresse = this.doc.createElement("adresse");
Node no_vor = this.doc.createElement("vorname");
Text te_vor = this.doc.createTextNode(mit.getVorname());
Node no_nach = this.doc.createElement("nachname");
Text te_nach = this.doc.createTextNode(mit.getNachname());
Node no_url = this.doc.createElement("urlaub");
Text te_url = this.doc.createTextNode(mit.getUrlaub());
Node no_abt = this.doc.createElement("abteilung");
Text te_abt = this.doc.createTextNode(mit.getAbteilung());
Node no_bon = this.doc.createElement("bonus");
Text te_bon = this.doc.createTextNode(String.valueOf(mit.getBonus()));
Node no_sta = this.doc.createElement("status");
Text te_sta = this.doc.createTextNode(mit.getStatus());
Node no_geh = this.doc.createElement("gehalt");
Text te_geh = this.doc.createTextNode(mit.getGehalt());
no_vor.appendChild(te_vor);
no_nach.appendChild(te_nach);
no_url.appendChild(te_url);
no_abt.appendChild(te_abt);
no_bon.appendChild(te_bon);
no_sta.appendChild(te_sta);
no_geh.appendChild(te_geh);
no_adresse.appendChild(no_vor);
no_adresse.appendChild(no_nach);
no_adresse.appendChild(no_url);
no_adresse.appendChild(no_abt);
no_adresse.appendChild(no_bon);
no_adresse.appendChild(no_sta);
no_adresse.appendChild(no_geh);
no_adrverw.appendChild(no_adresse);
}
this.doc.appendChild(no_adrverw);
}
private Vector get_dummy_data()
{
Vector vec = new Vector();
vec.addElement(new Mitarbeiter("vorname 1","nachname 1"));
vec.addElement(new Mitarbeiter("vorname 2","nachname 2"));
vec.addElement(new Mitarbeiter("vorname 3","nachname 3"));
return vec;
}
private Document get_new_document()
{
Document doc = null;
try
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
doc = builder.newDocument();
}
catch (Exception e) {e.printStackTrace();}
return doc;
}
private void save_document()
{
try
{
Transformer transformer = TransformerFactory.newInstance().newTransformer();
DOMSource source = new DOMSource( this.doc );
FileOutputStream os = new FileOutputStream( new File(this.filename) );
StreamResult result = new StreamResult( os );
transformer.transform( source, result );
}
catch(Exception e){}
}
public static void main(String[] args)
{
new XMLOutDemo();
}
}
package xmldemo;
public class Mitarbeiter
{
private String vorname = "";
private String nachname = "";
private int mitarbeiter_nr;
private String urlaub = "";
private String abteilung = "";
private float bonus = 0.0f;
private String status = "";
private String gehalt = "";
public Mitarbeiter(String vorname , String nachname)
{
this.vorname = vorname;
this.nachname = nachname;
}
public Mitarbeiter(float bonus , String nachname)
{
this.bonus = bonus;
this.nachname = nachname;
}
public Mitarbeiter()
{
}
public String getAbteilung() {
return abteilung;
}
public float getBonus() {
return bonus;
}
public int getMitarbeiter_nr() {
return mitarbeiter_nr;
}
public String getNachname() {
return nachname;
}
public String getUrlaub() {
return urlaub;
}
public String getVorname() {
return vorname;
}
public void setAbteilung(String string) {
abteilung = string;
}
public void setBonus(float f) {
bonus = f;
}
public void setMitarbeiter_nr(int i) {
mitarbeiter_nr = i;
}
public void setNachname(String string) {
nachname = string;
}
public void setUrlaub(String string) {
urlaub = string;
}
public void setVorname(String string) {
vorname = string;
}
public String getStatus() {
return status;
}
public void setStatus(String string) {
status = string;
}
public String getGehalt() {
return gehalt;
}
public void setGehalt(String s) {
gehalt = s;
}
public String toString()
{
return "Class Mitarbeiter: vorname "+this.getVorname() + " nachname " + this.getNachname() + " bonus " + this.getBonus();
}
}
class Line implements Paintable
{
int x, y, w, h;
Color color;
public Line( Color color, int x, int y, int w, int h )
{
this.color = color;
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
public void paint( Graphics g )
{
g.setColor( color );
g.drawLine( x, y, w, h );
}
}
Node no_x = this.doc.createElement("x");
Text te_x = this.doc.createTextNode(line.getX());
import java.awt.*;
public class Line implements Paintable
{
private int x, y, w, h;
private Color color;
public Line(Color color, int x, int y, int w, int h)
{
this.color = color;
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
public void paint( Graphics g )
{
g.setColor( color );
g.drawLine( x, y, w, h );
}
public Color getColor()
{
return color;
}
public int getH()
{
return h;
}
public int getW()
{
return w;
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
public void setColor(Color color)
{
this.color = color;
}
public void setH(int i)
{
h = i;
}
public void setW(int i)
{
w = i;
}
public void setX(int i)
{
x = i;
}
public void setY(int i)
{
y = i;
}
}
public void save(){
new XMLOutDemo();
}
import java.io.File;
import java.io.FileOutputStream;
import java.util.Iterator;
import java.util.Vector;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.Text;
public class XMLOutDemo
{
private String filename;
private Vector v_data;
private Document doc;
public XMLOutDemo()
{
this.v_data = PaintPanel.paints;
this.filename = "xmlout.xml";
this.doc = this.get_new_document();
this.build_document();
this.save_document();
System.out.println("fertig");
}
private void build_document()
{
Node no_adrverw = this.doc.createElement("adressenverwaltung");
Iterator it = this.v_data.iterator();
while (it.hasNext())
{
Line mit = (Line)it.next();
Node no_adresse = this.doc.createElement("adresse");
Node no_nach = this.doc.createElement("x");
Text te_nach = this.doc.createTextNode(String.valueOf(mit.getX()));
Node no_url = this.doc.createElement("Y");
Text te_url = this.doc.createTextNode(String.valueOf(mit.getY()));
Node no_abt = this.doc.createElement("H");
Text te_abt = this.doc.createTextNode(String.valueOf(mit.getH()));
Node no_bon = this.doc.createElement("W");
Text te_bon = this.doc.createTextNode(String.valueOf(mit.getW()));
no_nach.appendChild(te_nach);
no_url.appendChild(te_url);
no_abt.appendChild(te_abt);
no_bon.appendChild(te_bon);
no_adresse.appendChild(no_nach);
no_adresse.appendChild(no_url);
no_adresse.appendChild(no_abt);
no_adresse.appendChild(no_bon);
}
this.doc.appendChild(no_adrverw);
}
/* private Vector get_dummy_data()
{
Vector vec = new Vector();
vec.addElement(new Mitarbeiter("vorname 1","nachname 1"));
vec.addElement(new Mitarbeiter("vorname 2","nachname 2"));
vec.addElement(new Mitarbeiter("vorname 3","nachname 3"));
return vec;
}*/
private Document get_new_document()
{
Document doc = null;
try
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
doc = builder.newDocument();
}
catch (Exception e) {e.printStackTrace();}
return doc;
}
public void save_document()
{
try
{
Transformer transformer = TransformerFactory.newInstance().newTransformer();
DOMSource source = new DOMSource( this.doc );
FileOutputStream os = new FileOutputStream( new File(this.filename) );
StreamResult result = new StreamResult( os );
transformer.transform( source, result );
}
catch(Exception e){}
}
/* public static void main(String[] args)
{
new XMLOutDemo();
}*/
}
private void build_document()
{
Node no_adrverw = this.doc.createElement("mygraphics");
Iterator it = this.v_data.iterator();
while (it.hasNext())
{
Line mit = (Line)it.next();
Node no_adresse = this.doc.createElement("line");
Node no_nach = this.doc.createElement("x");
Text te_nach = this.doc.createTextNode(String.valueOf(mit.getX()));
Node no_url = this.doc.createElement("Y");
Text te_url = this.doc.createTextNode(String.valueOf(mit.getY()));
Node no_abt = this.doc.createElement("H");
Text te_abt = this.doc.createTextNode(String.valueOf(mit.getH()));
Node no_bon = this.doc.createElement("W");
Text te_bon = this.doc.createTextNode(String.valueOf(mit.getW()));
no_nach.appendChild(te_nach);
no_url.appendChild(te_url);
no_abt.appendChild(te_abt);
no_bon.appendChild(te_bon);
no_adresse.appendChild(no_nach);
no_adresse.appendChild(no_url);
no_adresse.appendChild(no_abt);
no_adresse.appendChild(no_bon);
no_adrverw.append(no_adresse);
}
this.doc.appendChild(no_adrverw);
}
Node no_nach = this.doc.createElement("x");
Text te_nach = this.doc.createTextNode(String.valueOf(mit.getX()));
Nein, da mußt du dir die RGB-Werte holen und einzeln abspeichern.Gibt es auch eine Umwandlung für den Typ Color ? So dass ich die Farbe speichern kann ?
if (ButtonPanel.print == "rectangle")
{
Rect mit = (Rect)it.next();
Node no_adresse = this.doc.createElement("rect");
}
else if (ButtonPanel.print == "line")
{
Line mit = (Line)it.next();
Node no_adresse = this.doc.createElement("line");
}
Node no_x = this.doc.createElement("x");
Text te_x = this.doc.createTextNode(String.valueOf(mit.getX()));
Node no_y = this.doc.createElement("y");
Text te_y = this.doc.createTextNode(String.valueOf(mit.getY()));
Node no_h = this.doc.createElement("h");
Text te_h = this.doc.createTextNode(String.valueOf(mit.getH()));
Node no_w = this.doc.createElement("w");
Text te_w = this.doc.createTextNode(String.valueOf(mit.getW()));
Object o = it.next();
if (o instanceof Line)
{
Line myLine = (Line)o;
}
.....
Object o = it.next();
Line myLine = null;
if (o instanceof Line)
{
myLine = (Line)o;javascript:
}
Node no_x = this.doc.createElement("x");
Text te_x = this.doc.createTextNode(String.valueOf(mit.getX()));
Node no_y = this.doc.createElement("y");
Text te_y = this.doc.createTextNode(String.valueOf(mit.getY()));
Node no_h = this.doc.createElement("h");
Text te_h = this.doc.createTextNode(String.valueOf(mit.getH()));
Node no_w = this.doc.createElement("w");
Text te_w = this.doc.createTextNode(String.valueOf(mit.getW()));
Jepp, mittels Polymorpie. Du erstellt dir ein Interface mit bestimmten Methoden, daß von allen deinen Klassen implementiert wird.Oder gibt es eine Möglichkeit, vorher ein unbestimmes Objekt anzulegen und das dann zu verwenden ?
interface SerializeGraphic
{
public int getH();
public int getW();
public int getX();
public int getY();
public int getNodeName();
}
public class Line implements SerializeGraphic
{
private int x, y, w, h;
private Color color;
public Line(Color color, int x, int y, int w, int h)
{
this.color = color;
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
public void paint(Graphics g)
{
g.setColor(color);
g.drawLine(x, y, w, h);
}
public Color getColor()
{
return color;
}
public void setColor(Color color)
{
this.color = color;
}
public int getH()
{
return h;
}
public int getW()
{
return h;
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
public int getNodeName()
{
return "line";
}
}
SerializeGraphic gr = (SerializeGraphic) it.next();
Node no_adresse = this.doc.createElement( gr.getNodeName());
Node no_nach = this.doc.createElement( gr.getX() );
public int getNodeName()
{
return "line";
}
SerializeGraphic gr = (SerializeGraphic) it.next();
Node no_adresse = this.doc.createElement( gr.getNodeName());
Node no_nach = this.doc.createElement( gr.getX() );
Alle deine Graphikobjekte müssen das Interface SerializeGraphic implementieren, dadurch sind sie alle kompatibel zu SerializeGraphic. Da du auf SerializeGraphic castest ist es egal ob du gerade ein Lineobjekt oder ein Rectobjekt verarbeitest, denn alle diese Objekte sind auch SerializeGraphicObjekte.Wie kann ich denn jetzt festlegen, dass "gr" beispielsweise "Rect" ist etc.
Den Namen holt er sich ja mit "getNodeName", aber wie lege ich fest, von welchem Objekt ?
try
{
FileOutputStream fos = new FileOutputStream("meinVector.xml");
BufferedOutputStream bos = new BufferedOutputStream(fos);
//XML Encoder erstellen
XMLEncoder XML = new XMLEncoder(bos);
//Den Vector schreiben
XML.writeObject(meinVector);
XML.close();
XML = null;
}
catch (IOException e)
{
}
Um einfach nur ein Objekt zu speichern ist XmlEncoder sicherlich die bessere Lösung. Aber wenn man die XmlDaten auch noch andersweitig verwenden will, ist eine Lösung mit DOM oder JDOM besser geeignet. Da man mit DOM mehr Kontrolle darüber hat wie die Daten gespeichert werden.Dann habe ich eine XML-Datei mit dem Inhalt des Vectors icon_smile.gif
Der XMLEncoder / XMLDecoder liegt in java.beans.
dann kannst du dein gefühlter Vector direkt in eine datei speichern soclass Line implements Paintable,Serializable{...
try {
FileOutputStream fs = new FileOutputStream("test.ser");
ObjectOutputStream os = new ObjectOutputStream(fs);
os.writeObject(deinvector);
os.close();
} catch (IOException e) {
System.err.println(e.toString());
}
try {
FileInputStream fs = new FileInputStream("test.ser");
ObjectInputStream is = new ObjectInputStream(fs);
Vector deinvector = (Vector)is.readObject();
System.out.println(time.toString());
is.close();
} catch (ClassNotFoundException e) {
System.err.println(e.toString());
} catch (IOException e) {
System.err.println(e.toString());
}