Hi, ich bins mal wieder!
Ich schreibe grade an einem XML-Encoder, allerdings klappt das ganze nicht so ganz. Der Encoder soll aus einem Java Programm eine XML-Datei schreiben. Das geht! Auf umgekehrtem Weg geht es auch schon aber mir ist es zu mühsam alle Programme in XML mit Hand zu schreiben. Also will ich es mit einem XML-Encoder einfach von Java nach XML "übersetzten" lassen. Hier meine Encoder-Code:
Code:
import java.beans.XMLEncoder;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class XMLencode
{
public static void main(String args[])
{
try
{
String output = "Programm.xml", input = "Programm.java";
Object o = new FileInputStream(input);
XMLEncoder e = new XMLEncoder(new FileOutputStream(output));
e.writeObject(o);
}
catch (FileNotFoundException e)
{
System.out.println(e.getMessage());
}
}
}
Es kommen folgende Fehlermedlungen:
java.lang.InstantiationException: java.io.FileInputStream
Continuing ...
java.lang.Exception: XMLEncoder: discarding statement XMLEncoder.writeObject(FileInputStream);
Continuing ...
Hier zum Verständniss die Codes für den Umgekehrten Weg (ein Java Programm als XML-Datei mit einem XML-Decoder ausführen):
Der Decoder:
Code:
import java.beans.XMLDecoder;
import java.io.FileInputStream;
import java.io.IOException;
class XMLdecode
{
public static void main(String args[])
{
String inputfile = "sample.xml";
try
{
XMLDecoder d = new XMLDecoder(new FileInputStream(inputfile));
Object o = d.readObject();
}
catch (IOException e)
{
System.out.println(e.getMessage());
}
}
}
Und die XML-Datei:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.4.1_03" class="java.beans.XMLDecoder">
<object class="javax.swing.JFrame">
<void property="size">
<object class="java.awt.Dimension">
<int>378</int>
<int>327</int>
</object>
</void>
<void property="contentPane">
<void method="add">
<object id="JButton0" class="javax.swing.JButton">
<string>Disable Text Field</string>
<void property="preferredSize">
<object class="java.awt.Dimension">
<int>336</int>
<int>34</int>
</object>
</void>
<void property="bounds">
<object class="java.awt.Rectangle">
<int>17</int>
<int>135</int>
<int>336</int>
<int>34</int>
</object>
</void>
<void method="addActionListener">
<object class="java.beans.EventHandler" method="create">
<class>java.awt.event.ActionListener</class>
<object id="JTextField0" class="javax.swing.JTextField">
<void property="preferredSize">
<object class="java.awt.Dimension">
<int>226</int>
<int>21</int>
</object>
</void>
<void property="bounds">
<object class="java.awt.Rectangle">
<int>63</int>
<int>201</int>
<int>226</int>
<int>21</int>
</object>
</void>
<void property="enabled">
<boolean>false</boolean>
</void>
<void property="document">
<void property="documentProperties">
<void id="Boolean0" method="get">
<string>filterNewlines</string>
</void>
</void>
</void>
</object>
<string>disable</string>
</object>
</void>
</object>
</void>
<void method="add">
<object idref="JTextField0"/>
</void>
<void method="add">
<object id="JButton1" class="javax.swing.JButton">
<string>Enable Text Field</string>
<void property="preferredSize">
<object class="java.awt.Dimension">
<int>339</int>
<int>36</int>
</object>
</void>
<void property="bounds">
<object class="java.awt.Rectangle">
<int>16</int>
<int>86</int>
<int>339</int>
<int>36</int>
</object>
</void>
<void method="addActionListener">
<object class="java.beans.EventHandler" method="create">
<class>java.awt.event.ActionListener</class>
<object idref="JTextField0"/>
<string>enable</string>
</object>
</void>
</object>
</void>
<void method="add">
<object id="JLabel0" class="javax.swing.JLabel">
<void property="preferredSize">
<object class="java.awt.Dimension">
<int>335</int>
<int>69</int>
</object>
</void>
<void property="bounds">
<object class="java.awt.Rectangle">
<int>21</int>
<int>15</int>
<int>335</int>
<int>69</int>
</object>
</void>
<void property="text">
<string>This is my First GUI Java Application!Nikos Fakos 2003.</string>
</void>
</object>
</void>
<void property="preferredSize">
<object class="java.awt.Dimension">
<int>370</int>
<int>300</int>
</object>
</void>
<void property="bounds">
<object class="java.awt.Rectangle">
<int>0</int>
<int>0</int>
<int>370</int>
<int>300</int>
</object>
</void>
<void property="layout">
<object id="SpringLayout0" class="javax.swing.SpringLayout">
<void method="addLayoutComponent">
<object idref="JLabel0"/>
<object class="java.beans.Expression">
<object idref="SpringLayout0"/>
<string>getConstraints</string>
<array class="java.lang.Object" length="1">
<void index="0">
<object idref="JLabel0"/>
</void>
</array>
<void property="value">
<void property="x">
<object class="javax.swing.Spring" method="constant">
<int>21</int>
</object>
</void>
<void property="y">
<object class="javax.swing.Spring" method="constant">
<int>15</int>
</object>
</void>
<void method="setConstraint">
<string>East</string>
<null/>
</void>
<void method="setConstraint">
<string>South</string>
<null/>
</void>
</void>
</object>
</void>
<void method="addLayoutComponent">
<object idref="JButton0"/>
<object class="java.beans.Expression">
<object idref="SpringLayout0"/>
<string>getConstraints</string>
<array class="java.lang.Object" length="1">
<void index="0">
<object idref="JButton0"/>
</void>
</array>
<void property="value">
<void property="x">
<object class="javax.swing.Spring" method="constant">
<int>17</int>
</object>
</void>
<void property="y">
<object class="javax.swing.Spring" method="constant">
<int>135</int>
</object>
</void>
<void method="setConstraint">
<string>East</string>
<null/>
</void>
<void method="setConstraint">
<string>South</string>
<null/>
</void>
</void>
</object>
</void>
<void method="addLayoutComponent">
<object idref="JTextField0"/>
<object class="java.beans.Expression">
<object idref="SpringLayout0"/>
<string>getConstraints</string>
<array class="java.lang.Object" length="1">
<void index="0">
<object idref="JTextField0"/>
</void>
</array>
<void property="value">
<void property="x">
<object class="javax.swing.Spring" method="constant">
<int>63</int>
</object>
</void>
<void property="y">
<object class="javax.swing.Spring" method="constant">
<int>201</int>
</object>
</void>
<void method="setConstraint">
<string>East</string>
<null/>
</void>
<void method="setConstraint">
<string>South</string>
<null/>
</void>
</void>
</object>
</void>
<void method="addLayoutComponent">
<object idref="JButton1"/>
<object class="java.beans.Expression">
<object idref="SpringLayout0"/>
<string>getConstraints</string>
<array class="java.lang.Object" length="1">
<void index="0">
<object idref="JButton1"/>
</void>
</array>
<void property="value">
<void property="x">
<object class="javax.swing.Spring" method="constant">
<int>16</int>
</object>
</void>
<void property="y">
<object class="javax.swing.Spring" method="constant">
<int>86</int>
</object>
</void>
<void method="setConstraint">
<string>East</string>
<null/>
</void>
<void method="setConstraint">
<string>South</string>
<null/>
</void>
</void>
</object>
</void>
</object>
</void>
</void>
<void property="glassPane">
<void property="bounds">
<object class="java.awt.Rectangle">
<int>0</int>
<int>0</int>
<int>370</int>
<int>300</int>
</object>
</void>
</void>
<void property="layeredPane">
<void property="bounds">
<object class="java.awt.Rectangle">
<int>0</int>
<int>0</int>
<int>370</int>
<int>300</int>
</object>
</void>
</void>
<void property="name">
<string>frame0</string>
</void>
<void property="visible">
<object idref="Boolean0"/>
</void>
</object>
</java>
Kann mir jemand helfen?
Das Hauptproblem liegt darin, denke ich zumindest, eine Java-Datei in ein Object umzuwandeln.
Danke.
MfG
MJA
Lesezeichen