Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to convert object to xml

3 views
Skip to first unread message

termechi

unread,
Jul 9, 2004, 4:10:18 AM7/9/04
to
hi ,
i need to now how i cant to convert a Java object to XML file
it its very important to me , because i not yet found a tutorial thet
give a good example to how implement .
thenk's , eyal

term...@gmail.com

Tor Iver Wilhelmsen

unread,
Jul 9, 2004, 4:57:10 AM7/9/04
to
term...@gmail.com (termechi) writes:

> i need to now how i cant to convert a Java object to XML file
> it its very important to me , because i not yet found a tutorial thet
> give a good example to how implement .

Use java.beans.XMLEncoder (new in 1.4)

Andrew Thompson

unread,
Jul 9, 2004, 5:57:14 AM7/9/04
to
On 09 Jul 2004 10:57:10 +0200, Tor Iver Wilhelmsen wrote:

> Use java.beans.XMLEncoder (new in 1.4)

e.g.
<sscce>
import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.beans.*;

/** Shows a simple example of saving objects as XML.
Requires Java 1.4+.
@author Andrew Thompson */
public class TestXMLWrite {

/** Writes this object as an XML file */
public static void writeObjectAsXML(Object o, String fileName)
throws FileNotFoundException {

File f = new File(fileName);
XMLEncoder e = new XMLEncoder(
new BufferedOutputStream(
new FileOutputStream(f)));
e.writeObject(o);
e.close();
}

public static void main(String args[]) throws Exception {
Button b = new Button("Open");
writeObjectAsXML(b, "button.xml");

JTree jt = new JTree();
writeObjectAsXML(jt, "tree.xml");
}
}
</sscce>

HTH

--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology

0 new messages