[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

JAXB error "doesnt contain ObjectFactory.class or jaxb.index"

Posted on 2008-02-12
2
Medium Priority
?
13,902 Views
1 Endorsement
Last Modified: 2010-04-21
Hi all,

I've got javax.xml.bind.JAXBException: "jaxb.Foo" doesnt contain ObjectFactory.class or jaxb.index

I am trying to do an unmarshall with JaxB, I have the following:

Any idea?

class Foo {
  @XmlElement
  String a;
  @XmlElement(required=true)
  String b;
  @XmlElement
  String c;
}
 
-------------------------------------------------------
myJaxb.java
-------------------------------------------------------
 
    JAXBContext jc = JAXBContext.newInstance( "jaxb.Foo" );
      
          Unmarshaller u = jc.createUnmarshaller();
      
          Foo fooObj = (Foo)u.unmarshal(
              new FileInputStream( "foo.xml" ) ); 
 
---------------------------------------
foo.xml
----------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<Foo>
         <a>1</a>
         <b>2</b>
         <c>3</c>
</Foo>

Open in new window

1
Comment
Question by:pzaprianov
2 Comments
 
LVL 3

Accepted Solution

by:
schybert earned 2000 total points
ID: 20882502
If I'm not mistaken the JAXBContext.newInstance method that takes a String is expecting a package name. I would use the one that takes an array of Class objects and pass in Foo.class. Alos if you're using the unmarshal method that just takes one argument you need to add the @XmlRootElement annotation to Foo. Oh, and by default JAXB will accept <foo> in the xml, not <Foo>.
@XmlRootElement 
class Foo {
  @XmlElement
  String a;
  @XmlElement(required=true)
  String b;
  @XmlElement
  String c;
}
 
-------------------------------------------------------
myJaxb.java
-------------------------------------------------------
 
    JAXBContext jc = JAXBContext.newInstance(new Class[] {jaxb.Foo.class});
      
          Unmarshaller u = jc.createUnmarshaller();
      
          Foo fooObj = (Foo)u.unmarshal(
              new FileInputStream( "foo.xml" ) ); 
 
---------------------------------------
foo.xml
----------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<foo>
         <a>1</a>
         <b>2</b>
         <c>3</c>
</foo>

Open in new window

0
 

Author Closing Comment

by:pzaprianov
ID: 31430374
Excellent solution, just what i was looking for, i am going to post another question and I hope you can help me again. Thank you
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month8 days, 13 hours left to enroll

613 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question