JAXB code snippets for beginners

What is JAXB

JAXB is a Java library used for reading and writing XML files. Unlike DOM or SAX, JAXB is a high level library that maps XML directly to Java Objects. You point JAXB to an XML file and it returns you a Java object representing the XML. While JAXB is easy to use, it may take some time to get started. I’ve documented everything that you need to know, with code snippets, to help you start using JAXB in a few minutes.

Step 1/2: Mapping your schema XSD to Java

The first step to create Java classes which map to your schema. The XJC compiler does exactly that. Here’s a reference to the XJC compiler. However if you are using Maven, there is a maven jaxb plugin which automatically generates the Java mapping by defining a few snippets in your pom.xml. The following section shows you how to use JAXB from Maven.

Using JAXB from Maven: configure your pom.xml to automatically run the Maven JAXB plugin

To use JAXB from Maven, you need to add the JAXB plugin repository information and the Maven JAXB plugin declaration to your pom.xml.

<pluginRepositories>
 <pluginRepository>
  <id>maven2-repository.dev.java.net</id>
  <name>Java.net Repository for Maven</name>
  <url>http://download.java.net/maven/2/</url>
  <layout>default</layout>
 </pluginRepository>
</pluginRepositories>

Change sitemap.xsd below to your schema.xsd name. Also, change the output package “com.vineetmanohar.sitemap.jaxb” to a package of your choice. Generally, this is a brand new package with no other classes in it.

<build>
 <plugins>
  <plugin>
   <!-- jaxb plugin -->
   <groupId>org.jvnet.jaxb2.maven2</groupId>
   <artifactId>maven-jaxb2-plugin</artifactId>
   <executions>
    <execution>
     <id>sitemap</id>
     <goals>
      <goal>generate</goal>
     </goals>

     <configuration>
      <args>
       <param>-npa</param>
      </args>

      <!-- the package for the generated java classes -->
      <generatePackage>com.vineetmanohar.sitemap.jaxb</generatePackage>
      <npa>true</npa>
      <!-- include the following schemas only; by default all *.xsd files are processed -->
      <schemaIncludes>
       <include>sitemap.xsd</include>
      </schemaIncludes>

      <!-- whether old output should be removed, this field should generally be set to "true" -->
      <removeOldOutput>true</removeOldOutput>
      <!-- generate lots of output -->
      <verbose>true</verbose>
     </configuration>
    </execution>
   </executions>
  </plugin>
 </plugins>
</build>

Note: If you are using JDK 1.5, then you need to add the javax.xml.bind classes as dependency. Not needed for JDK 1.6.

    <!-- only for JDK 1.5, not required for JDK 1.6 -->
    <dependency>
      <artifactId>jaxb-api</artifactId>
      <groupId>javax.xml.bind</groupId>
      <version>2.1</version>
    </dependency>

    <dependency>
      <artifactId>jaxb-impl</artifactId>
      <groupId>com.sun.xml.bind</groupId>
      <version>2.1.3</version>
    </dependency>

Output and Generated Classes

The JAXB plugin by default runs in the “generate-sources” phase. To run the JAXB plugin, simply run “mvn generate-sources”, or any phase which occurs after “generate-sources”, like “compile”.

mvn compile

This will run the JAXB plugin and generates Java classes for your schema (for sitemap.xsd in the above example). The output java package is “com.vineetmanohar.sitemap.jaxb”, as specified in the plugin configuration above. Here’s how the output looks after running the plugin.

target/generated-sources
target/generated-sources/xjc
target/generated-sources/xjc/META-INF
target/generated-sources/xjc/META-INF/sun-jaxb.episode
target/generated-sources/xjc/com
target/generated-sources/xjc/com/vineetmanohar
target/generated-sources/xjc/com/vineetmanohar/sitemap
target/generated-sources/xjc/com/vineetmanohar/sitemap/jaxb
target/generated-sources/xjc/com/vineetmanohar/sitemap/jaxb/ObjectFactory.java
target/generated-sources/xjc/com/vineetmanohar/sitemap/jaxb/Urlset.java
target/generated-sources/xjc/com/vineetmanohar/sitemap/jaxb/TChangeFreq.java
target/generated-sources/xjc/com/vineetmanohar/sitemap/jaxb/TUrl.java

Note that the generated classes live in the target directory. You should not modify them or check them into your source control system. These classes are temporary and will be automatically generated when your run maven compile.

Step 2/2: Using the JAXB generated classes from your Java code

Once the code is generated, you are now ready to use the code from your Java class. Here are 3 ways of using JAXB from your java code.

1. Basic way: A basic way is to create a wrapper file which hides all the JAXB details and provides an easy to use interface. See the SitemapJAXBWrapper.java file to see an example implementation. I don’t recommend this option, however, as a beginner you should try it out as it helps you understand the JAXB API. The following option, option #2 is a more preferred approach.

2. Preferred way: This is basically option 1 refactored to hide all the JAXB details. Download this re-usable JAXBWrapper.java class and use that as a wrapper class. The JAXBWrapper class gives you an easy way to do XML to Java and Java to XML conversions in just 2-3 lines of code. All you need to do it pass 2 parameters, the schema URL, and the classes that you want to bind. The classes are the classes that were previously generated using XJC or the Maven JAXB plugin above. In this example we are trying to read a sitemap.xml file and load it into the java class Urlset.java.You won’t find the Urlset.java on my Google Code project because it is a temporary file which is automatically generated when needed and never checked it. Here’s a sample code.

Create a wrapper

JaxbWrapper<Urlset> sitemapJaxbWrapper = new JaxbWrapper<Urlset>(getClass().getResource("/sitemap.xsd"), Urlset.class);

XML to Java

Urlset urlset = sitemapJaxbWrapper.xmlToObject(getClass().getResourceAsStream("/sample-sitemap.xml"));

Java to XML

Urlset urlset = new com.vineetmanohar.sitemap.jaxb.ObjectFactory().createUrlset();
String xml = sitemapJaxbWrapper.objectToXml(urlset);

3. Using Spring framework: The third way to use JAXB is through Spring OXM classes. Here’s a reference documentation. I won’t go into the details, but the idea is that you create a JaxB2Marshaller bean in your spring config, then inject the bean in the class where you want to use it.

FAQ

How to generate classes for multiple schemas in different packages

Add one <execution> step per schema. Add a different <id> per execution. Add a unique <generateDirectory> in the configuration section. By default the <generateDirectory> value is target/generated-sources/xjc. You should set it such that each one of them is different. For example target/generated-sources/xjc1, target/generated-sources/xjc2 etc. Full reference of JAXB configuration is here.

<executions>
 <execution>
  <id>id1</id>
  <goals>
   <goal>generate</goal>
  </goals>
  <configuration>
   <args>
    <param>-npa</param>
   </args>
   <generateDirectory>target/generated-sources/xjc1</generateDirectory>
   <generatePackage>some.package1</generatePackage>
  <schemaIncludes>
   <include>schema1.xsd</include>
  </schemaIncludes>
  <forceRegenerate>false</forceRegenerate>
  <removeOldOutput>true</removeOldOutput>
  <verbose>true</verbose>
  </configuration>
 </execution>

 <execution>
  <id>id2</id>
  <goals>
   <goal>generate</goal>
  </goals>
  <configuration>
   <args>
    <param>-npa</param>
   </args>
   <generateDirectory>target/generated-sources/xjc2</generateDirectory>
   <generatePackage>some.package2</generatePackage>
   <schemaIncludes>
    <include>schema2.xsd</include>
   </schemaIncludes>
   <forceRegenerate>false</forceRegenerate>
   <removeOldOutput>true</removeOldOutput>
   <verbose>true</verbose>
  </configuration>
 </execution>
</executions>

Are JAXB Marshaller and Unmarshaller thread-safe?

No. Marshaller and Unmarshaller are not thread-safe because they use a non-thread safe TransformerFactory. Therefore, you need to create a new Marshaller and Unmarshaller object per invocation. The JAXBWrapper already takes care of that detail.

Why do I get a “unable to marshal type because it is missing an @XmlRootElement annotation]”

If you get this stack trace when running JAXB, see the following solution.

[com.sun.istack.SAXException2: unable to marshal type "org.foo.xmlbindings.FooType" as an element because it is missing an @XmlRootElement annotation]
 at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:304)
 at com.sun.xml.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:230)
 at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:96)
 at Caused by: com.sun.istack.SAXException2: unable to marshal type "org.foo.xmlbindings.FooType" as an element because it is missing an @XmlRootElement annotation
 at com.sun.xml.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:226)
 at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot(ClassBeanInfoImpl.java:267)
 at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:472)
 at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:301)
 ... 26 more
... Removed 22 stack frames
[com.sun.istack.SAXException2: unable to marshal type "org.foo.xmlbindings.FooType" as an element because it is missing an @XmlRootElement annotation]
 at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:304)
 at com.sun.xml.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:230)
 at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:96)
 at
Caused by: com.sun.istack.SAXException2: unable to marshal type "org.foo.xmlbindings.FooType" as an element because it is missing an @XmlRootElement annotation
 at com.sun.xml.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:226)
 at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot(ClassBeanInfoImpl.java:267)
 at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:472)
 at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:301)
 ... 26 more
... Removed 22 stack frames

Solution: Make sure that the root element is an anonymous complex type and not an instance of a defined type.

Right root element example:

  <xsd:element name="foo">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="bar" type="p:barType" />
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>

Wrong root element example:

  <xsd:element name="foo" type="fooType">
  </xsd:element>

  <xsd:complexType name="fooType">
    <xsd:sequence>
      <xsd:element name="bar" type="p:barType" />
    </xsd:sequence>
  </xsd:complexType>

Why do I get Maven Jaxb package-info syntax error

[INFO] Applying extractor for language: java
[INFO] ------------------------------------------------------------------------
[ERROR] FATAL ERROR
[INFO] ------------------------------------------------------------------------
[INFO] syntax error @[8,7] in file:xmlbindings/package-info.java
[INFO] ------------------------------------------------------------------------
[INFO] Trace
com.thoughtworks.qdox.parser.ParseException: syntax error @[8,7] in file:xmlbindings/package-info.java
        at com.thoughtworks.qdox.parser.impl.Parser.yyerror(Parser.java:638)
        at com.thoughtworks.qdox.parser.impl.Parser.yyparse(Parser.java:747)
        at com.thoughtworks.qdox.parser.impl.Parser.parse(Parser.java:619)
        at com.thoughtworks.qdox.JavaDocBuilder.addSource(JavaDocBuilder.java:300)
        at com.thoughtworks.qdox.JavaDocBuilder.addSource(JavaDocBuilder.java:316)
        at com.thoughtworks.qdox.JavaDocBuilder.addSource(JavaDocBuilder.java:312)
        at com.thoughtworks.qdox.JavaDocBuilder$1.visitFile(JavaDocBuilder.java:369)
        at com.thoughtworks.qdox.directorywalker.DirectoryScanner.walk(DirectoryScanner.java:43)
        at com.thoughtworks.qdox.directorywalker.DirectoryScanner.walk(DirectoryScanner.java:34)
        at com.thoughtworks.qdox.directorywalker.DirectoryScanner.walk(DirectoryScanner.java:34)
        at com.thoughtworks.qdox.directorywalker.DirectoryScanner.walk(DirectoryScanner.java:34)
        at com.thoughtworks.qdox.directorywalker.DirectoryScanner.walk(DirectoryScanner.java:34)
        at com.thoughtworks.qdox.directorywalker.DirectoryScanner.walk(DirectoryScanner.java:34)
        at com.thoughtworks.qdox.directorywalker.DirectoryScanner.walk(DirectoryScanner.java:34)
        at com.thoughtworks.qdox.directorywalker.DirectoryScanner.walk(DirectoryScanner.java:34)
        at com.thoughtworks.qdox.directorywalker.DirectoryScanner.scan(DirectoryScanner.java:52)
        at com.thoughtworks.qdox.JavaDocBuilder.addSourceTree(JavaDocBuilder.java:366)
        at org.apache.maven.tools.plugin.extractor.java.JavaMojoDescriptorExtractor.discoverClasses(JavaMojoDescriptorExtractor.java:614)
        at org.apache.maven.tools.plugin.extractor.java.JavaMojoDescriptorExtractor.execute(JavaMojoDescriptorExtractor.java:581)
        at org.apache.maven.tools.plugin.scanner.DefaultMojoScanner.populatePluginDescriptor(DefaultMojoScanner.java:87)
        at org.apache.maven.plugin.plugin.AbstractGeneratorMojo.execute(AbstractGeneratorMojo.java:137)
        at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:447)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:539)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalWithLifecycle(DefaultLifecycleExecutor.java:480)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java:459)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:311)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:278)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:143)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:333)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:126)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:282)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:585)
        at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315)
        at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
        at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)
        at org.codehaus.classworlds.Launcher.main(Launcher.java:375)
[INFO] ------------------------------------------------------------------------

Cause: A file called package-info.java gets generated in the output package. Here's the content of the file.

//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.0.2-b01-fcs
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// Any modifications to this file will be lost upon recompilation of the source schema.
// Generated on: 2008.06.11 at 01:20:24 PM EDT
//

@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.vineetmanohar.com", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package xmlbindings;

The error is on the period immediately following javax (@javax.)

Workaround: add the no package info generation flag to xjc

      <plugin>
        <!-- new maven jaxb -->
        <groupId>org.jvnet.jaxb2.maven2</groupId>
        <artifactId>maven-jaxb2-plugin</artifactId>

        <executions>
          <execution>
            <goals>
              <goal>generate</goal>
            </goals>
            <configuration>
              <args>
                <param>-npa</param>
              </args>

Why do I get Maven jaxb UnmarshalException unexpected element?

javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"project"). Expected elements are (none)
	at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:525)
	at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:199)
	at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:194)
	at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:71)
	at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:922)
	at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:366)
	at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:347)
	at com.sun.xml.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:99)
	at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:533)
	at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:330)
	at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDispatcher.scanRootElementHook(XMLNSDocumentScannerImpl.java:779)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(XMLDocumentFragmentScannerImpl.java:1794)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:368)
	at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:834)
	at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764)
	at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:148)
	at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1242)
	at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:199)
	at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:172)
	at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137)
	at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:184)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:585)
	at org.junit.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99)
	at org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81)
	at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
	at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75)
	at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45)
	at org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:71)
	at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35)
	at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42)
	at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
	at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

Solution

Make sure that correct class is passed to unmarshall.

        javax.xml.bind.JAXBContext jc = javax.xml.bind.JAXBContext
                .newInstance();
        javax.xml.bind.Unmarshaller u = jc.createUnmarshaller();

        JAXBElement<Foo> root = u.unmarshal(new StreamSource(is),
                Foo.class);
        Foo foo = root.getValue();

        return project;
    }

Why do I get "Project not known to this context error"?

javax.xml.bind.JAXBException: Foo is not known to this context
	at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getBeanInfo(JAXBContextImpl.java:510)
	at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.getBeanInfo(UnmarshallerImpl.java:502)
	at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:178)
	at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:229)
        ...
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:585)
	at org.junit.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99)
	at org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81)
	at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
	at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75)
	at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45)
	at org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:71)
	at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35)
	at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42)
	at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
	at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

Solution
Specify the root object when creating the context.

   javax.xml.bind.JAXBContext jc = javax.xml.bind.JAXBContext
                .newInstance(Foo.class);

Reference

Related posts:

  1. Maven FAQ and code snippets for beginners
  2. Access to www.w3.org DTDs blocked from Java
  3. How to copy bean properties with a single line of code
  4. Spring Quartz code snippets for beginners
  5. Maven Docbook for beginners

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Get Adobe Flash playerPlugin by wpburn.com wordpress themes