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

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

pzaprianovAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

schybertCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pzaprianovAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.