tsaowe
tsaowe

Reputation: 45

How to convert a Java object to an XML string, and conversely convert XML to a Java object?

Assume a Java object:

Object obj = new Object();

How do I store this object in the database so to persist this status, and after I store it, it should be easy to convert to a Java object like when I store it?

Upvotes: 2

Views: 7752

Answers (5)

user_3380739
user_3380739

Reputation: 1254

Here is the simplest and fastest code by abacus-common:

Account account = N.fill(Account.class);
String xml = N.toXML(account);
N.println(xml); // <account><id>6264304841028291043</id><gui>33acdcbe-fd5b-49</gui><emailAddress>19c1400a-97ae-43</emailAddress><firstName>67922557-8bb4-47</firstName><middleName>7ef242c9-8ddf-48</middleName><lastName>1ec6c731-a3fd-42</lastName><birthDate>1480444055841</birthDate><status>1444930636</status><lastUpdateTime>1480444055841</lastUpdateTime><createTime>1480444055841</createTime></account>
Account account2 = N.fromXML(Account.class, xml);
assertEquals(account, account2);

Declaration: I'm the developer of abacus-common.

Upvotes: 0

bdoughan
bdoughan

Reputation: 149007

Note: I'm the EclipseLink JAXB (MOXy) lead, and a member of the JAXB 2 (JSR-222) expert group.


How to convert a Java object to an XML string, and conversely convert XML to a Java object?

With JAXB you can marshal an Object to a java.io.StringWriter to produce a String, and leverage a java.io.StringReader to unmarshal an Object from a String.


then I want to store this object in the database

The Java EE solution is to use a JPA implementation (TopLink/EclipseLink, Hibernate, Open JPA, etc.) to store the object in the database, and a JAXB implementation (TopLink/EclipseLink, Metro, Apache JaxMe, etc.) to convert the object to/from XML.

Since you will be persisting your objects to a database you need to be sure that your object-to-XML mapping (OXM) solution takes the following things into consideration:

Bidirectional Relationships

Object models used with object-to-relational mapping (ORM) solutions often have bidirectional relationships. You need to ensure this doesn't send your OXM layer into a loop. MOXy JAXB for example has the @XmlInverseReference extension to handle this:

Lazy Relationships

Many ORM solutions offer a mechanism to to have relationships between objects lazily realized. Some like TopLink/EclipseLink modify the Java bytecodes to add the logic to the "get" methods. You will need an OXM solution that is able to leverage both fields and properties. In MOXy or any JAXB implementation this is done via @XmlAccessorType:

Compound Keys/Embedded Key Classes

Database tables often have compound keys. If these keys will also be leveraged for intra-document references then you will need an OXM solution that can handle it. MOXy offers @XmlKey and @XmlJoinNodes:

Upvotes: 2

bigGuy
bigGuy

Reputation: 1744

Also, JAXB. (Hello World example)

Upvotes: 4

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 298898

Any of these will do:

Or just plain JDK XMLEncoder / XMLDecoder (introductory article), but that's a royal pain in the donkey if you ask me.

Upvotes: 5

npinti
npinti

Reputation: 52185

You can use XStream which is very easy and straightforward as shown in this 2 minute tutorial.

Upvotes: 2

Related Questions