TechnoBuzz

A Techno Blog, mainly about Java

JAXME2

JAXB is now available as part of the Java Web Services developer pack from Sun. While JAXB was in Beta, you could download Sun’s implemementation of JAXB seperately . It appears that is not the case anymore. Here is my page on sun. I have posted some messages to the various Sun forums on the problem I am having using JAXB. Here is an article on user feedback with JAXB. I may look into other JAXB implementations. Specifically, Apache’s open source JAXME2. Another one is XMLBeans.

I chose JAXME.

JAXME Manual
Featured article on JAXME

How to download: go to jaxme download page and scroll to bottom for distributions (src or bin).

I grabbed the two example files (build.xml and xsd file) from the article and placed them inside the new jaxme directory that was just created when the binaries tar file was extracted.


Edit the build file to set where the jar files are located (lib).
Invoke ‘ant generate’ and the neccesary files get generated in the generated folder.
As well as generating the source code, its creates two files (configuartion.xml and jaxb.properties).



XML file (schema instance):

< ?xml version=”1.0″ encoding=”UTF-8″? >
<stu:Student xmlns:stu=”http://dw.ibm.com/jaxme/student&#8221;
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; >
<stu:firstName>stu:firstName</stu:firstName >
<stu:lastName>stu:lastName</stu:lastName >
<stu:address>
<stu:name>stu:name</stu:name >
<stu:street>stu:street</stu:street >
<stu:city>stu:city</stu:city >
<stu:state>stu:state</stu:state >
<stu:zip>1</stu:zip>
</stu:address >

Read XML:

package com.ibm.dw.jaxme.student;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.StringWriter;
import java.net.MalformedURLException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import org.xml.sax.InputSource;

public class Main {

public static Student getAddress(InputSource pSource)
throws JAXBException {
JAXBContext context =
JAXBContext.newInstance(“com.ibm.dw.jaxme.student”);
Unmarshaller unmarshaller = context.createUnmarshaller();
return (Student) unmarshaller.unmarshal(pSource);
}
public static void main(String[] args) throws Exception {
File f = new File(“src/student.xml”);
InputSource isource = new InputSource(new FileInputStream(f));
isource.setSystemId(f.toURL().toString());
Student student = getAddress(isource);
System.out.println(student.getFirstName());
}
}

Advertisements

August 4, 2004 - Posted by | XML

No comments yet.

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

%d bloggers like this: