开发者

What is the opposite of JAXB? i.e. generating XML FROM classes?

开发者 https://www.devze.com 2022-12-22 13:53 出处:网络
I am currently designing a solution to a problem I have. I need to dynamically generate an XML file on the fly using Java objects, in the same way JAXB generates Java classes from XML files, however t

I am currently designing a solution to a problem I have. I need to dynamically generate an XML file on the fly using Java objects, in the same way JAXB generates Java classes from XML files, however the opposite direction. Is there something out there already like this开发者_如何学Python?

Alternatively, a way in which one could 'save' a state of java classes.

The goal I am working towards is a dynamically changing GUI, where a user can redesign their GUI in the same way you can with iGoogle.


You already have the answer. It's JAXB! You can annotate your classes and then have JAXB marshal them to XML (and back) without the need to create an XML schema first.

Look at https://jaxb.dev.java.net/tutorial/section_6_1-JAXB-Annotations.html#JAXB%20Annotations to get started.


I don't know, if this is exactly what you're looking for, but there's the java.beans.XMLEncoder:

XMLEncoder enc = new XMLEncoder(new FileOutputStream(file));
enc.writeObject(obj);
enc.close();

The result can then be loaded by XMLDecoder:

XMLDecoder dec = new XMLDecoder(new FileInputStream(file));
Object obj = dec.readObject();
dec.close();


"generate xml from java objects:"

try xtream. Here's what is said on the tin:

No mappings required. Most objects can be serialized without need for specifying mappings.
Requires no modifications to objects.
Full object graph support

For saving java object state:
Serialization is the way to do this in Java

0

精彩评论

暂无评论...
验证码 换一张
取 消