How to Generate Jaxb Classes from Xsd

How to generate JAXB classes from XSD?

XJC is included in the bin directory in the JDK starting with Java SE 6. For an example see:

  • http://blog.bdoughan.com/2010/09/processing-atom-feeds-with-jaxb.html

The contents of the blog are the following:

Processing Atom Feeds with JAXB
Atom is an XML format for representing web feeds. A standard format allows reader applications to display feeds from different sources. In this example we will process the Atom feed for this blog.

Demo

In this example we will use JAXB to convert the Atom XML feed corresponding to this blog to objects and then back to XML.

import java.io.InputStream;
import java.net.URL;
import javax.xml.bind.*;
import javax.xml.transform.stream.StreamSource;
import org.w3._2005.atom.FeedType;

public class Demo {

public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance("org.w3._2005.atom");

Unmarshaller unmarshaller = jc.createUnmarshaller();
URL url = new URL("http://bdoughan.blogspot.com/atom.xml");
InputStream xml = url.openStream();
JAXBElement<feedtype> feed = unmarshaller.unmarshal(new StreamSource(xml), FeedType.class);
xml.close();

Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(feed, System.out);
}

}

JAXB Model

The following model was generated by the schema to Java compiler (XJC). I have omitted the get/set methods and comments to save space.

xjc -d generated http://www.kbcafe.com/rss/atom.xsd.xml

package-info

@XmlSchema(
namespace = "http://www.w3.org/2005/Atom",
elementFormDefault = XmlNsForm.QUALIFIED)
@XmlAccessorType(XmlAccessType.FIELD)
package org.w3._2005.atom;

import javax.xml.bind.annotation.*;

CategoryType

package org.w3._2005.atom;

import java.util.*;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.*;
import javax.xml.namespace.QName;

@XmlType(name = "categoryType")
public class CategoryType {
@XmlAttribute(required = true)
protected String term;

@XmlAttribute
@XmlSchemaType(name = "anyURI")
protected String scheme;

@XmlAttribute
protected String label;

@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlSchemaType(name = "anyURI")
protected String base;

@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name = "language")
protected String lang;

@XmlAnyAttribute
private Map<QName, String> otherAttributes = new HashMap<QName, String>();
}

Content Type

package org.w3._2005.atom;

import java.util.*;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.*;
import javax.xml.namespace.QName;

@XmlType(name = "contentType", propOrder = {"content"})
public class ContentType {
@XmlMixed
@XmlAnyElement(lax = true)
protected List<Object> content;

@XmlAttribute
protected String type;

@XmlAttribute
@XmlSchemaType(name = "anyURI")
protected String src;

@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlSchemaType(name = "anyURI")
protected String base;

@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name = "language")
protected String lang;

@XmlAnyAttribute
private Map<QName, String> otherAttributes = new HashMap<QName, String>();
}

DateTimeType

package org.w3._2005.atom;

import java.util.*;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.*;
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.namespace.QName;

@XmlType(name = "dateTimeType", propOrder = {"value"})
public class DateTimeType {
@XmlValue
@XmlSchemaType(name = "dateTime")
protected XMLGregorianCalendar value;

@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlSchemaType(name = "anyURI")
protected String base;

@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name = "language")
protected String lang;

@XmlAnyAttribute
private Map<QName, String> otherAttributes = new HashMap<QName, String>();
}

EntryType

package org.w3._2005.atom;

import java.util.*;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.*;
import javax.xml.namespace.QName;

@XmlType(name = "entryType", propOrder = {"authorOrCategoryOrContent"})
public class EntryType {
@XmlElementRefs({
@XmlElementRef(name = "id", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "rights", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "summary", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "title", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "author", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "source", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "updated", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "category", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "content", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "published", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "contributor", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "link", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class)
})
@XmlAnyElement(lax = true)
protected List<Object> authorOrCategoryOrContent;

@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlSchemaType(name = "anyURI")
protected String base;

@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name = "language")
protected String lang;

@XmlAnyAttribute
private Map<QName, String> otherAttributes = new HashMap<QName, String>();
}

FeedType

package org.w3._2005.atom;

import java.util.*;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.*;
import javax.xml.namespace.QName;

@XmlType(name = "feedType", propOrder = {"authorOrCategoryOrContributor"})
public class FeedType {
@XmlElementRefs({
@XmlElementRef(name = "link", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "updated", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "category", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "rights", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "contributor", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "title", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "id", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "generator", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "icon", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "subtitle", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "author", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "entry", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "logo", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class)
})
@XmlAnyElement(lax = true)
protected List<Object> authorOrCategoryOrContributor;

@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlSchemaType(name = "anyURI")
protected String base;

@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name = "language")
protected String lang;

@XmlAnyAttribute
private Map<QName, String> otherAttributes = new HashMap<QName, String>();
}

GeneratorType

package org.w3._2005.atom;

import java.util.*;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.*;
import javax.xml.namespace.QName;

@XmlType(name = "generatorType", propOrder = {"value"})
public class GeneratorType {
@XmlValue
protected String value;

@XmlAttribute
@XmlSchemaType(name = "anyURI")
protected String uri;

@XmlAttribute
protected String version;

@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlSchemaType(name = "anyURI")
protected String base;

@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name = "language")
protected String lang;

@XmlAnyAttribute
private Map<QName, String> otherAttributes = new HashMap<QName, String>();
}

IconType

package org.w3._2005.atom;

import java.util.*;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.*;
import javax.xml.namespace.QName;

@XmlType(name = "iconType", propOrder = {"value"})
public class IconType {
@XmlValue
@XmlSchemaType(name = "anyURI")
protected String value;

@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlSchemaType(name = "anyURI")
protected String base;

@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name = "language")
protected String lang;

@XmlAnyAttribute
private Map<QName, String> otherAttributes = new HashMap<QName, String>();
}

IdType

package org.w3._2005.atom;

import java.util.*;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.*;
import javax.xml.namespace.QName;

@XmlType(name = "idType", propOrder = {"value"})
public class IdType {
@XmlValue
@XmlSchemaType(name = "anyURI")
protected String value;

@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlSchemaType(name = "anyURI")
protected String base;

@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name = "language")
protected String lang;

@XmlAnyAttribute
private Map<QName, String> otherAttributes = new HashMap<QName, String>();
}

LinkType

package org.w3._2005.atom;

import java.math.BigInteger;
import java.util.*;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.*;
import javax.xml.namespace.QName;

@XmlType(name = "linkType", propOrder = {"content"})
public class LinkType {
@XmlValue
protected String content;

@XmlAttribute(required = true)
@XmlSchemaType(name = "anyURI")
protected String href;

@XmlAttribute
protected String rel;

@XmlAttribute
protected String type;

@XmlAttribute
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name = "NMTOKEN")
protected String hreflang;

@XmlAttribute
protected String title;

@XmlAttribute
@XmlSchemaType(name = "positiveInteger")
protected BigInteger length;

@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlSchemaType(name = "anyURI")
protected String base;

@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name = "language")
protected String lang;

@XmlAnyAttribute
private Map<QName, String> otherAttributes = new HashMap<QName, String>();
}

LogoType

package org.w3._2005.atom;

import java.util.*;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.*;
import javax.xml.namespace.QName;

@XmlType(name = "logoType", propOrder = {"value"})
public class LogoType {
@XmlValue
@XmlSchemaType(name = "anyURI")
protected String value;

@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlSchemaType(name = "anyURI")
protected String base;

@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name = "language")
protected String lang;

@XmlAnyAttribute
private Map<QName, String> otherAttributes = new HashMap<QName, String>();
}

PersonType

package org.w3._2005.atom;

import java.util.*;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.*;
import javax.xml.namespace.QName;

@XmlType(name = "personType", propOrder = {"nameOrUriOrEmail"})
public class PersonType {
@XmlElementRefs({
@XmlElementRef(name = "email", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "name", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "uri", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class)
})
@XmlAnyElement(lax = true)
protected List<Object> nameOrUriOrEmail;

@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlSchemaType(name = "anyURI")
protected String base;

@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name = "language")
protected String lang;

@XmlAnyAttribute
private Map<QName, String> otherAttributes = new HashMap<QName, String>();
}

SourceType

package org.w3._2005.atom;

import java.util.*;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.*;
import javax.xml.namespace.QName;

@XmlType(name = "sourceType", propOrder = {"authorOrCategoryOrContributor"})
public class SourceType {
@XmlElementRefs({
@XmlElementRef(name = "updated", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "category", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "subtitle", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "logo", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "generator", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "icon", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "title", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "id", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "author", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "contributor", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "link", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "rights", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class)
})
@XmlAnyElement(lax = true)
protected List<Object> authorOrCategoryOrContributor;

@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlSchemaType(name = "anyURI")
protected String base;

@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name = "language")
protected String lang;

@XmlAnyAttribute
private Map<QName, String> otherAttributes = new HashMap<QName, String>();
}

TextType

package org.w3._2005.atom;

import java.util.*;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.*;
import javax.xml.namespace.QName;

@XmlType(name = "textType", propOrder = {"content"})
public class TextType {
@XmlMixed
@XmlAnyElement(lax = true)
protected List<Object> content;

@XmlAttribute
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
protected String type;

@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlSchemaType(name = "anyURI")
protected String base;

@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name = "language")
protected String lang;

@XmlAnyAttribute
private Map<QName, String> otherAttributes = new HashMap<QName, String>();
}

UriType

package org.w3._2005.atom;

import java.util.*;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.*;
import javax.xml.namespace.QName;

@XmlType(name = "uriType", propOrder = {"value"})
public class UriType {
@XmlValue
@XmlSchemaType(name = "anyURI")
protected String value;

@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlSchemaType(name = "anyURI")
protected String base;

@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name = "language")
protected String lang;

@XmlAnyAttribute
private Map<QName, String> otherAttributes = new HashMap<QName, String>();
}

How to generate JAXB data format from XSD schema?

Generating classes from XSD with CLI tool

You can use xjc command-line tool that comes pre-installed with at JDK 8 to generate jaxb classes.

Example:

xjc material.xsd

# With groupId
xjc -p <groupId> material.xsd

# With groupId and bindings configuration file
xjc -p <groupId> -b bindings.xjb material.xsd

Generating classes from XSD using maven plugin

Alternatively you can use maven plugin to do the same. By default the plugin will look for schema xsd files from src/main/xsd and bindings xjb files from src/main/xjb

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.5.0</version>
<executions>
<execution>
<id>xjc</id>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<packageName>com.example.group</packageName>
</configuration>
</plugin>
</plugins>
</build>

If you're using JDK 11 or later you'll also have to include couple of related dependencies that are no longer included in the JDK.

<dependencies>
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>2.3.3</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-xjc</artifactId>
<version>2.3.6</version>
</dependency>
</dependencies>

With these maven should generate the classes to target/generated-sources/jaxb folder after running mvn clean install. With maven plugin you're better off creating separate api-project for these and adding it as a dependency for your camel integration project.

Usage in camel

You can use jaxb with camel by creating JaxbDataFormat using JAXBContext instance.

JAXBContext jaxbContext = JAXBContext.newInstance(Materials.class);
JaxbDataFormat jaxbDataformat = new JaxbDataFormat(jaxbContext);

from("direct:marshalMaterials")
.routeId("marshalMaterials")
.marshal(jaxbDataformat)
.log("${body}");

Since you're querying a database that by default returns list of maps, you'll have to convert it to appropriate Jaxb object. You can use generated ObjectFactory class to generate different jaxb class instances.

With JDK 11 you might also need following dependencies

<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>2.3.3</version>
</dependency>

<!-- versions obtained from dependency-management camel-bom -->
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
</dependency>

Namespace change from javax to jakarta

More recent versions of jaxb are using jakarta namespace instead of javax but at the time of writing use of the jakarta namespace is a bit problematic as the jaxb2-maven-plugin version 3.0.0+ isn't yet available in maven central repository and while there are some alternatives they're still somewhat work in progress.

If you the xsd doesn't change often one could potentially just generate the classes and change the namespaces manually to jakarta.

Bindings XJB files

With bindings xjb files you can tweak how classes get generated like change class or property names, prevent nested class mess etc.

Example: Empty binding file template

<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="2.1"
xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd">

...

</jaxb:bindings>

How to create pojo classes from XSD?

My recommendation is to go with JAXB.

I have tested it in eclipse, works well for me. My suggestion is try generating the POJO from command line or with the help of eclipse. Once successful configure it with maven to generate the POJO build time.

There are several tutorials to learn this, please follow the below link(s) based upon your preference:

  • Generate POJO Class from XSD in Eclipse
  • Generate POJO class from XSD Schema command line
  • Generate POJO Classes from XSD using XJC Maven Plugin

Also the youtube links:

  • Youtube video tutorial
  • Youtube tutorial using maven

I hope it helps!

Feel free to comment if you encounter any issue.

Generate Java Classes from .xsd schema in eclipse

I got this to work by adding jaxb jar files to my project's classpath. I included: jaxb-core-2.3.0.jar, jaxb-impl-2.3.0.jar, and jaxb-xjc-2.3.0.jar. The impl jar was only added when I started using a bindings file which required me to enable vendor extensions. Otherwise the core and xjc jars were all I needed. I included links to where I got my jars in case you do not already have them

http://repo1.maven.org/maven2/com/sun/xml/bind/jaxb-xjc/2.3.0

http://repo1.maven.org/maven2/com/sun/xml/bind/jaxb-core/2.3.0

https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-impl/2.3.0

Generate classes from xsd in Java ( Jaxb )

I managed to do it !
I will show my solution, in case someone have the same problem.
i used ProcessBuilder in a method in order to execute the command from java :

public class FromXmlToJava implements Runnable {

private final InputStream inputStream;

 FromXmlToJava ( InputStream inputStream)
{

this.inputStream = inputStream;

}

private BufferedReader getBufferedReader(InputStream is)
{
return new BufferedReader(new InputStreamReader(is));
}

public void run() {

BufferedReader br = getBufferedReader(inputStream);
String ligne = "";
try
{
while ( (ligne = br.readLine() )!= null)
{

System.out.println(ligne);
}

} catch (IOException e)

{

e.printStackTrace();
}

}

public static class Main {
// chemin de l'emplacement des classes generées

public static final String CHEMIN = "C:\\Users\\****";
public static void main(String[] args) {
try {
ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/C",
"C:\\Users\\***\\xjc.bat ***.xsd");

// modifier le repertoire d'execution de la commande

pb.directory(new File(CHEMIN));

/* Map env = pb.environment();
for (Entry entry : env.entrySet()) {
System.out.println(entry.getKey() + " : " + entry.getValue());
}

env.put("MonArg", "Valeur");
*/
Process p = pb.start();
FromXmlToJava fluxSortie = new FromXmlToJava(p.getInputStream());
FromXmlToJava fluxErreur = new FromXmlToJava(p.getErrorStream());
new Thread(fluxSortie).start();
new Thread(fluxErreur).start();
p.waitFor();

} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

}

Generate Java Classes from XSD with default package name and XSD namespace

You can tell Maven to set the generated classes in a given package.

Use <generatePackage>desired.package</generatePackage> inside the execution tag.

It is well documented here.

Generate Java classes from .XSD files...?

JAXB does EXACTLY what you want. It's built into the JRE/JDK starting at 1.6



Related Topics



Leave a reply



Submit