File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes XML and Related Technologies and the fly likes java.beans.XMLEncoder Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "java.beans.XMLEncoder" Watch "java.beans.XMLEncoder" New topic


Drew Lane
Ranch Hand

Joined: May 13, 2001
Posts: 296
Does anyone have experience using java.beans.XMLEncoder?

I'm trying to serialize a third party class to XML.

Although the class implements serializable, I can't get XMLEncoder to work on it.

Does the class have to be a full Java bean to use XMLEncoder?

How else could I encode this class to XML?


Russ Tennant

Joined: Jul 27, 2004
Posts: 1
See Using XMLEncoder for a description on how to use the XMLEncoder.

Keep in mind that there are several bugs related to encoding / decoding java beans with the xml encoder and decoder. You can get an idea by searching the website. For example, java.beans.Statement is not thread safe. Most of these issues are being resolved in java 2 SDK version 1.5.

If you want to encode an Object that is not strictly a java bean, you can create a custom PersistenceDelegate that creates Statements / Expressions that describe how to instantiate a new instance of the object and also mutate that instance so that it is equal to the object being encoded. The Statements / Expressions are used to write out the xml document. By doing this, you are basically telling the decoder how to create a new instance of an object and mutate that instance so that it is in the same state as it was when you encoded it. See
Using XMLDecoder for details on how this works.
Drew Lane
Ranch Hand

Joined: May 13, 2001
Posts: 296
Heh...this thing is somewhat complicated for what I need.

What API should I use in Java for simple XML output?

I want something easy like this pseudo code:

XMLDocument xmlDoc = new XMLDocument("myxml.xml");
XMLElement books = new XMLElement("books");
XMLAttribute size = new XMLAttribute("1");
books.addChild("Gone with the Wind");

Is there anything built in for this?

I'm running Java 1.4.1

Dave Hewy
Ranch Hand

Joined: Aug 21, 2003
Posts: 93
I've been looking at Betwixt (Apache project) which is great for converting beans to XML, but I'm having problems going the other way.
Look at my post for an example on how to use this.

Peter den Haan
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
You might be interested in JDom or Dom4J.

- Peter
Drew Lane
Ranch Hand

Joined: May 13, 2001
Posts: 296
Why is there not something built-in to Java for doing this after all this time?

I agree. Here's the link:
subject: java.beans.XMLEncoder
It's not a secret anymore!