jaxb, xjc with java 1.4

 

While doing a xml project, I was stuck with an old java 1.4 that I could not  update😦.

So I used jaxb  with java 1.4 which lacks annotation. Little information on the intertubs so I made this post on howto use jaxb with java 1.4 . Hope it is useful.

You will need  old [JAXB1] libs , compatible with java 1.4.

 

//// ANT COMMAND

$ pwd
/xjc-java14

$ant xjc-compile-1
Buildfile: build.xml

xjc-compile-1:
[echo] Compiling the schema (source version 1.0)…
[mkdir] Created dir: /xjc-java14/src/org/xjctest1
[xjc] Compiling file:/xjc-java14/xsd/sample.xsd
[xjc] removing old output files
[xjc] Writing output to /xjc-java14/src

BUILD SUCCESSFUL
Total time: 3 seconds

///// ANT FILE – please: edit it to your tastes.

<?xml version=”1.0″ standalone=”yes”?>
<project basedir=”.” default=”xjc-compile-1″>
<description>
test xjc: generate class suitable for older java, without annotations (eg 1.4)
</description>
<property name=”jaxb.home” value=”jaxb/” />
<path id=”classpath”>
<pathelement path=”src” />
<pathelement path=”classes” />
<pathelement path=”schemas” />
<fileset dir=”${jaxb.home}” includes=”lib/*.jar” />
</path>
<taskdef name=”xjc” classname=”com.sun.tools.xjc.XJCTask”>
<classpath refid=”classpath” />
</taskdef>

<!–xjc compile source version 1–>
<target name=”xjc-compile-1″ description=”Compile xsd 1.0″>
<echo message=”Compiling the schema (source version 1.0)…” />
<mkdir dir=”src/org/xjctest1″ />
<xjc extension=”true” schema=”xsd/sample.xsd” source=”1.0″ removeOldOutput=”yes” package=”org.xjctest1″ target=”src”>
<produces dir=”src/org/xjctest1″ includes=”* impl/*” />
</xjc>
</target>

<!–xjc compile source version 2–>
<target name=”xjc-compile-2″ description=”Compile xsd 2.0″>
<echo message=”Compiling the schema (source version 2.0)…” />
<mkdir dir=”src/org/xjctest2″ />
<xjc extension=”true” schema=”xsd/sample.xsd” source=”2.0″ removeOldOutput=”yes” package=”org.xjctest2″ destdir=”src”>
<produces dir=”src/org/xjctest2″ />
</xjc>
</target>

<target name=”clean” description=”Deletes all the generated artifacts.”>
</target>
</project>

 

//// XSD –  sample.xsd

<?xml version=”1.0″?>
<xsd:schema xmlns:xsd=”http://www.w3.org/2001/XMLSchema”&gt;
<xsd:element name=”helloWorld” type=”HelloWorldType”/>
<xsd:complexType name=”HelloWorldType”>
<xsd:attribute name=”msg” type=”xsd:string”/>
</xsd:complexType>
</xsd:schema>

//// TEST CALSS – App1.java

package org.xjctest;

import java.io.File;
import java.util.Date;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import org.xjctest1.HelloWorld;
import org.xjctest1.ObjectFactory;

//simple class for testing jaxb/xjc
public class App1 {

/**
* @param args
*/
public static void main(String[] args) {
try {

testVersion1();

} catch (Exception e) {
e.printStackTrace();
}

}

//depends on java >= 1.4
private static void testVersion1() throws JAXBException {
JAXBContext context = JAXBContext.newInstance(“org.xjctest1”);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(true));
marshaller.setProperty(Marshaller.JAXB_ENCODING, “ISO-8859-1”);

HelloWorld hello = new ObjectFactory().createHelloWorld();
hello.setMsg(“hello world 1.0″);

String outFile=”bin/out1.xml”;
marshaller.marshal(hello, new File(outFile));
p(“xjc version 1.0 saved in “+ outFile);
}

//poorman logger
public static void p(Object o){
System.out.println(“[“+new Date()+”] “+o.toString());
}
}

//// RESULT – XJC

/xjc-java14/src/org/xjctest1$ ls
bgm.ser  HelloWorld.java  HelloWorldType.java  impl  jaxb.properties  ObjectFactory.java

//// RESULT – XML

out1.xml

<?xml version=”1.0″ encoding=”ISO-8859-1″ standalone=”yes”?>
<helloWorld msg=”hello world 1.0″/>

//// JAXB DIR
$ ls xjc-java14/jaxb/lib
activation.jar  jaxb1-impl.jar  jaxb-api.jar  jaxb-impl.jar  jaxb-xjc.jar  jsr173_1.0_api.jar

////LINKS:

[XJC-ANT] http://jaxb.java.net/nonav/2.0.2/docs/xjcTask.html

[JAXB1] download old jaxb libs: http://jaxb.java.net/2.1.2/ (version I used)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s