JaxB automatic parsing from XML to Java classes -


i new jaxb. question following: using jaxb, possible automatic mapping xml file java object? starting xml file, there generate java class annotations jaxb relaitve?

it indeed possible. however, you'll need xsd rather xml file. there tools out there (trang, instance) can infer xsd 1 or more example xml files.

take account generating xsd tool might inaccurate results if xml sample isn't complete, or if schema can't represented in single xml file (exclusive elements, etc).

once have xsd, use xjc in order generate marshaller/unmarshaller classes.

xjc myxsd.xsd 

this generate annotated classes jaxb use marshalling/unmarshalling. notice have coded these classes yourself. once have them, use them in code:

file file = new file("myfile.xml"); jaxbcontext jaxbcontext = jaxbcontext.newinstance(myrootelement.class); unmarshaller jaxbunmarshaller = jaxbcontext.createunmarshaller(); myrootelement element = (myrootelement) jaxbunmarshaller.unmarshal(file); 

Comments