Srs, Gerei um XSD
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
jxb:version="2.0">
<xsd:element name="Greetings" type="GreetingListType"/>
<xsd:complexType name="GreetingListType">
<xsd:sequence>
<xsd:element name="Greeting" type="GreetingType"
maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="GreetingType">
<xsd:sequence>
<xsd:element name="Text" type="xsd:string"/>
</xsd:sequence>
<xsd:attribute name="language" type="xsd:language"/>
</xsd:complexType>
</xsd:schema>
E compilei com mo jaxb.
Ele me gerou as classes para bind ai em seguida gerei um codigo para exportação (gerei naquelas, isto é um tutorial que to seguindo) :
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import br.com.foxtec.tipos.GreetingListType;
import br.com.foxtec.tipos.GreetingType;
import br.com.foxtec.tipos.ObjectFactory;
public class Hello {
private ObjectFactory of;
private GreetingListType grList;
public Hello(){
of = new ObjectFactory();
grList = of.createGreetingListType();
}
public void make( String t, String l ){
GreetingType g = of.createGreetingType();
g.setText( t );
g.setLanguage( l );
grList.getGreeting().add( g );
}
public void marshal() {
try {
JAXBElement gl =
of.createGreetings( grList );
JAXBContext jc = JAXBContext.newInstance("hello");
Marshaller m = jc.createMarshaller();
m.marshal( gl, System.out );
} catch( JAXBException jbe ){
jbe.printStackTrace();
}
}
}
ai em seguida fiz uma classe separada com método main para testar tudo.
public class Teste {
/**
* @param args
*/
public static void main(String[] args) {
Hello h = new Hello();
h.make( "Bonjour, madame", "fr" );
h.make( "Hey, you", "en" );
h.marshal();
}
}
Porém estu recebendo essa Exception :
javax.xml.bind.JAXBException: "hello" doesnt contain ObjectFactory.class or jaxb.index
at com.sun.xml.internal.bind.v2.ContextFactory.createContext(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at javax.xml.bind.ContextFinder.newInstance(Unknown Source)
at javax.xml.bind.ContextFinder.find(Unknown Source)
at javax.xml.bind.JAXBContext.newInstance(Unknown Source)
at javax.xml.bind.JAXBContext.newInstance(Unknown Source)
at javax.xml.bind.JAXBContext.newInstance(Unknown Source)
at Hello.marshal(Hello.java:31)
at Teste.main(Teste.java:11)
Alguem tem uma ideia do que pode estar gerando esse problema ?