Your starting point should be the XSD file. You can use an online XSD generator to build one from your XML: http://www.freeformatter.com/xsd-generator.html
You can also use a tool like Trang http://www.thaiopensource.com/relaxng/trang.html
Or if you want to build your own XSD generator, look at this link Generate XSD programmatically in java
Finally, here are some JAXB examples that may help ...
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.SchemaOutputResolver;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamResult;
import org.xml.sax.InputSource;
import com.sun.codemodel.JCodeModel;
import com.sun.tools.xjc.api.S2JJAXBModel;
import com.sun.tools.xjc.api.SchemaCompiler;
import com.sun.tools.xjc.api.XJC;
public class Demo {
public static void buildClasses(File xsdFile) throws IOException {
SchemaCompiler sc = XJC.createSchemaCompiler();
sc.parseSchema(new InputSource(xsdFile.toURI().toString()));
S2JJAXBModel model = sc.bind();
JCodeModel cm = model.generateCode(null, null);
cm.build(new File("."));
}
public static final void printFile(File file) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
}
public static void main(String[] args) {
try {
Example example;
File xmlFile = new File("example.xml");
File xsdFile = new File("example.xsd");
JAXBContext jaxbContext = JAXBContext.newInstance(Example.class);
////////////////////////////////////////////////////////////////////////////////////
// USE THIS CODE TO BUILD YOUR XML FROM AN EXISTING INSTANTIATED JAVA CLASS
////////////////////////////////////////////////////////////////////////////////////
System.out.println("===========================================");
example = new Example();
example.id = 123;
example.name = "Constantin";
example.dob = new SimpleDateFormat("yyyy-MM-dd").parse("1971-01-13");
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(example, xmlFile);
printFile(xmlFile);
System.out.println("===========================================");
////////////////////////////////////////////////////////////////////////////////////
// USE THIS CODE TO BUILD YOUR XSD FROM AN EXISTING JAVA CLASS
////////////////////////////////////////////////////////////////////////////////////
System.out.println("===========================================");
jaxbContext.generateSchema(new SchemaOutputResolver() {
@Override
public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
StreamResult result = new StreamResult(new FileOutputStream(xsdFile));
result.setSystemId(xsdFile.getAbsolutePath());
return result;
}
});
printFile(xsdFile);
System.out.println("===========================================");
////////////////////////////////////////////////////////////////////////////////////
// USE THIS CODE TO INSTANTIATE A JAVA CLASS FROM AN EXISTING XML FILE
////////////////////////////////////////////////////////////////////////////////////
System.out.println("===========================================");
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
example = (Example) jaxbUnmarshaller.unmarshal(xmlFile);
System.out.println("[" + example.getId() + "][" + example.getName() + "][" + example.getDOB() + "]");
System.out.println("===========================================");
////////////////////////////////////////////////////////////////////////////////////
// USE THIS CODE TO BUILD JAVA CLASSES FROM EXISTING XSD FILE
////////////////////////////////////////////////////////////////////////////////////
buildClasses(xsdFile);
}
catch (JAXBException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
catch (ParseException e) {
e.printStackTrace();
}
}
}
@XmlRootElement
class Example {
int id;
String name;
Date dob;
@XmlElement
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
@XmlElement
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
@XmlElement
public void setDOB(Date dob) {
this.dob = dob;
}
public Date getDOB() {
return dob;
}
}
You may need to download some dependency jars to make this work : https://jaxb.java.net/2.2.11/