• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.beans.XMLEncoder

 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Thanks,

Drew
 
Russ Tennant
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 bugs.sun.com 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
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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");
books.addAttribute(size);
xmlDoc.writeFile();

Is there anything built in for this?

I'm running Java 1.4.1

Drew
 
Dave Hewy
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Dave
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might be interested in JDom or Dom4J.

- Peter
 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is there not something built-in to Java for doing this after all this time?

Drew
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic