简体   繁体   中英

Generating java binding classes using XML file without XSD schema file? Using any Castor or JAXB framework

I have a requirement to create java model classes (binding classes) from XML files. Unfortunately I don't have XSD schema. There are many already existing XML's.

Strangely , Castor or JAXB both use XSD schema to generate binding model classes. What if you don't have a schema? Not all XML are created using Schema!

You could use a schema inference tool like Trang . It can produce xsd from xml . You could then tweak the generated schema or just feed it straight to JAXB/Castor etc.

The reason that XML binding technologies do not generate Java classes from XML is that it is an extra level of complexity:

  • Is that single XML element really a single element or a collection of size 1?
  • The data in the sample XML looks like an int, but is String content allowed?

You have a couple of options. The first is you can use a tool to generate an XML schema from the XML and then generate the classes from that. Or you could annotate your existing classes to map them to XML:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM