How do I use JAXB with Netbeans?

I downloaded books.xsd and books.xml from http://java.sun.com/developer/technicalArticles/WebServices/jaxb.

Generated the classes using ./xjc.sh -p test.jaxb /Users/Me/Temp/books.xsd -d /Users/Me/Temp/work.
It generated BookCategoryType.java, BookType.java, Collection.java, ObjectFactory.java, inside /Users/Me/Temp/work/test/jaxb directory. Then I compile them: javac *.java, generating BookCategoryType.class, BookType$Authors.class, BookType$Promotion.class, BookType.class, Collection$Books.class, Collection.class, ObjectFactory.class, inside the same directory.

How do I reference these classes from Netbeans? I am writing:

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.security.Security;

           JAXBContext jc = JAXBContext.newInstance("test.jaxb");
           Unmarshaller u = jc.createUnmarshaller();

           Collection collection= (Collection) unmarshaller.unmarshal(new File( "books.xml"));
           
I am aware that Collection shold be test.jaxb.Collection. How can I do it? It just doesn't find it (it tries to import java.util.Collection, and that is not the case.

Thanks in advance          

cinglezAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
objectsConnect With a Mentor Commented:
I already answered the original question
0
 
objectsCommented:
you need to include /Users/Me/Temp/work/ in the projects build path
0
 
cinglezAuthor Commented:
Ok, I've done that. Now, what I need is to turn the contents of the XML file into an java object. Could someone provide me an example on how to do that? I've been trying to do:

           JAXBContext jc = JAXBContext.newInstance("test.jaxb");
           Unmarshaller u = jc.createUnmarshaller();
           Object o = u.unmarshal(new File ("books.xml"));

But when I run the executable (java -jar Teste.jar, I always get

javax.xml.bind.JAXBException: "test.jaxb" doesnt contain ObjectFactory.class or jaxb.index
      at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:186)
      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:597)
      at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:128)
      at javax.xml.bind.ContextFinder.find(ContextFinder.java:290)
      at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:372)
      at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:337)
      at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:244)
      at Teste.Status.listaStatus(Status.java:41)
      at Teste.Status.main(Status.java:73)

Any ideas?

Thanks
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
objectsCommented:
are the classes and jaxb.properties included in the jar?
0
 
cinglezAuthor Commented:
Yes they were. I will close this thread, because I solved my problems using Apache Axis instead of JAXB directly.

Thanks anyway
0
 
cinglezAuthor Commented:
Ok, I agree
0
 
objectsCommented:
thanks mate
0
All Courses

From novice to tech pro — start learning today.