Monday, October 22, 2007

Java To XML Technologies

I am trying to find the most suitable Java to XML utility that will provide easy to read XML that defines a Java object. Here are some sample ones below:

1. XML Encoder

sample:

JFrame class

XML:

<?xml version="1.0" encoding="UTF-8"?>
<java version="1.0" class="java.beans.XMLDecoder">
<object class="javax.swing.JFrame">
<void property="name">
<string>frame1</string>
</void>
<void property="bounds">
<object class="java.awt.Rectangle">
<int>0</int>
<int>0</int>
<int>200</int>
<int>200</int>
</object>
</void>
<void property="contentPane">
<void method="add">
<object class="javax.swing.JButton">
<void property="label">
<string>Hello</string>
</void>
</object>
</void>
</void>
<void property="visible">
<boolean>true</boolean>
</void>
</object>
</java>
advantages:
  • completely comprehensive
  • part of the Java API
disadvantages:
  • verbose
2. JSX - Java Serialization to XML

sample XML:
N/A

advantages:

disadvantages:
  • not mature product
  • not free
  • no sample provided
3. XStream

sample:

Java:
public class Person {
private String firstname;
private String lastname;
private PhoneNumber phone;
private PhoneNumber fax;
// ... constructors and methods
}

public class PhoneNumber {
private int code;
private String number;
// ... constructors and methods
}
XML:
<person>
<firstname>Joe</firstname>
<lastname>Walnes</lastname>
<phone>
<code>123</code>
<number>1234-456</number>
</phone>
<fax>
<code>123</code>
<number>9999-999</number>
</fax>
</person>

advantages:
  • no mappings required

disadvantages:

4. Castor

References: Java Boutique Tutorial: Java To XML and Back Again with Castor XML

sample:

Java:
private List myList = new ArrayList();

public void setMyList(List myList) {
this.myList = myList;
}

public List getMyList() {
return this.myList;
}

public Person(String name, String email, String phone, List myList) {
this.name = name;
this.email = email;
this.phone = phone;
this.myList = myList;
}
XML:
<?xml version="1.0" encoding="UTF-8"?>
<person>
<myList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:type="java:java.util.String">
List#1
</myList>
<myList xsi:type="java:java.util.String">
List#2
</myList>
<email>prokhorenko@gmail.com</email>
<name>Olexandr Prokhorenko</name>
<phone>123-555-1234</phone>
</person>
advantages:
  • comprehensive
  • mature product

disadvantages:
  • requires a mapping file

5. Java2XML

References: Java Boutique Tutorial: Java To XML and Back Again with Castor XML

sample:

Java:
public class HelloWorld
extends Thread
{
private int N;

private long sleep;

public HelloWorld(int N, long sleep)
{
this.N = N;
this.sleep = sleep;
}

public void run()
{
for (int i = 0; i < hw =" new"> XML:
<!-- Generated by Java2XML http://java2xml.dev.java.net/ -->
<java-source-program>
<java-class-file name="helloworld.java">
<import module="java.util.*"/>
<class name="HelloWorld" visibility="public">
<superclass name="Thread"/>
<field name="N" visibility="private">
<type primitive="true" name="int"/>
</field>
<field name="sleep" visibility="private">

<type primitive="true" name="long"/>
</field>
<constructor visibility="public" name="HelloWorld">
<formal-arguments>
<formal-argument name="N">
<type primitive="true" name="int"/>
</formal-argument>
<formal-argument name="sleep">
<type primitive="true" name="long"/>

</formal-argument>
</formal-arguments>
<assignment-expr op="=">
<lvalue>
<field-access field="N">
<this/>
</field-access>
</lvalue>
<var-ref name="N"/>

</assignment-expr>
<assignment-expr op="=">
<lvalue>
<field-access field="sleep">
<this/>
</field-access>
</lvalue>
<var-ref name="sleep"/>
</assignment-expr>

</constructor>
<method name="run" visibility="public">
<type name="void" primitive="true"/>
<formal-arguments/>
<block>
<loop kind="for">
<init>
<local-variable name="i">
<type primitive="true" name="int"/>

<literal-number kind="interger" value="0"/>
</local-variable>
</init>
<test>
<binary-expr op="<">
<var-ref name="i"/>
<field-access field="N">
<this/>
</field-access>

</binary-expr>
</test>
<update>
<unary-expr op="++" post="true">
<var-ref name="i"/>
</unary-expr>
</update>
<block>
<send message="println">

<target>
<field-access field="out">
<var-ref name="System"/>
</field-access>
</target>
<arguments>
<literal-string value=""Hello World""/>
</arguments>
</send>

<try>
<block>
<send message="sleep">
<target>
<var-ref name="Thread"/>
</target>
<arguments>
<var-ref name="sleep"/>
</arguments>

</send>
</block>
<catch>
<formal-argument name="e">
<type name="InterruptedException"/>
</formal-argument>
<block>
<send message="printStackTrace">
<target>

<var-ref name="e"/>
</target>
<arguments/>
</send>
</block>
</catch>
</try>
</block>
</loop>

</block>
</method>
<method name="main" visibility="public" static="true">
<type name="void" primitive="true"/>
<formal-arguments>
<formal-argument name="args">
<type name="String" dimensions="1"/>
</formal-argument>
</formal-arguments>

<block>
<local-variable name="hw">
<type name="HelloWorld"/>
<new>
<type name="HelloWorld"/>
<arguments>
<literal-number kind="interger" value="10"/>
<literal-number kind="interger" value="1000"/>
</arguments>

</new>
</local-variable>
<send message="start">
<target>
<var-ref name="hw"/>
</target>
<arguments/>
</send>
</block>

</method>
</class>
</java-class-file>
</java-source-program>
advantages:
  • comprehensive
  • mature product
  • free

disadvantages:
  • complicated XML
6. Jato

References: Javaworld Article: Jato: The new kid on the open source block

disadvatages:

  • fairly new product
7.  JiBX

advantages

disadvantages:
  • requires binding file to define the XML 

1 comment:

Anonymous said...

Who knows where to download XRumer 5.0 Palladium?
Help, please. All recommend this program to effectively advertise on the Internet, this is the best program!