Autor Beitrag
|M|Ich|Er|
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 34


Morfik Web OS, Delphi 6 EE
BeitragVerfasst: Sa 12.05.07 17:12 
hallo @ all

ich will eine html-seite speichern und laden.

könnt ihr mir sagen was dieser code macht, sieht mir mehr nach php aus, als xml.
wo wird letztendlich die seite gespeichert und geladen?

ausblenden volle Höhe XML-Daten
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
/** 
* @author 
* 
* this class serialize an object to xml by using the 
* xstream.codehaus.org package and write/read it to/from hdd
*/
public class ObjectToXmlToFile {
  private XStream xstream;
  /**
   * init stream for serialise to xml
   * methode use is internal, therefore private declaration
   */
  private void initStream(){
    xstream = new XStream(new DomDriver());
  }  
  /**
   * methode to setup file with filename and serialize
   * data inside this file
   * @param filename is filename on hdd
   * @param data is data to save
   */
  public void saveGame(String filename, Object data){
    initStream();
    File file = new File(filename);
    try{
      FileWriter toFile = new FileWriter(file);
      toFile.write(xstream.toXML(data));
      toFile.flush();
      toFile.close();
    }
    catch(Exception e){e.printStackTrace();}
  }
  /**
   * returns the unserialized object from the given file
   * @param file
   * @return
   */
  public Object loadGame(String filename){
    Object temp=null;
    File file = new File(filename);
    initStream();
    try{
      BufferedReader reader = new BufferedReader(new FileReader(file));
      temp = (Object) xstream.fromXML(reader);
    }
    catch(Exception e){e.printStackTrace();}
    return temp;
  }
 
  public static void main(String[] args) {
 
    String test = "das ist ein test-string";
 
    ObjectToXmlToFile saver = new ObjectToXmlToFile();
    saver.saveGame("data.xml", test);
 
    System.out.println(saver.loadGame("data.xml"));
  }
}
Marco D.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2750

Windows Vista
Delphi 7, Delphi 2005 PE, PHP 4 + 5 (Notepad++), Java (Eclipse), XML, XML Schema, ABAP, ABAP OO
BeitragVerfasst: Sa 12.05.07 17:19 
Also das ist definitiv kein PHP. Das ist Java.
Wo hast du das her? Ist vielleicht etwas aus einem Browsergame.

_________________
Pascal keeps your hand tied. C gives you enough rope to hang yourself. C++ gives you enough rope to shoot yourself in the foot
|M|Ich|Er| Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 34


Morfik Web OS, Delphi 6 EE
BeitragVerfasst: Sa 12.05.07 17:44 
Zitat:
Also das ist definitiv kein PHP. Das ist Java.

achso ok, dann hab ich das falsch interpretiert..

Zitat:
Wo hast du das her? Ist vielleicht etwas aus einem Browsergame.

hab des beim herumsurfen gefunden, die internetseite weiss ich leider nicht mehr

kann mir vielleicht jemand sagen was dieser codeteil bringt, bzw. genau macht?
alias5000
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2145

WinXP Prof SP2, Ubuntu 9.04
C/C++(Code::Blocks, VS.NET),A51(Keil),Object Pascal(D2005PE, Turbo Delphi Explorer) C# (VS 2008 Express)
BeitragVerfasst: Sa 12.05.07 18:52 
Im Quelltext steht:

* this class serialize an object to xml by using the
* xstream.codehaus.org package and write/read it to/from hdd


Ich kann damit also Objekte serialisieren. D.h. auf der Festplatte speichern und von ihr wieder laden.

Gruß
alias5000

_________________
Programmers never die, they just GOSUB without RETURN
|M|Ich|Er| Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 34


Morfik Web OS, Delphi 6 EE
BeitragVerfasst: So 13.05.07 11:20 
ausblenden Quelltext
1:
2:
* this class serialize an object to xml by using the 
* xstream.codehaus.org package and write/read it to/from hdd



Zitat:
Ich kann damit also Objekte serialisieren. D.h. auf der Festplatte speichern und von ihr wieder laden.


genau das wollt ich wissen, dankeschön!