简体   繁体   中英

creating a .xml file from .xsd with java

I'm a quite new to java world and I have a requirement of generating an .xml file from an .xsd file

I did some research and found that 'jaxb' could do it. And I found some example too , but the problem is, almost all the examples uses 'xjc' tool to do this. But I want a way to do this through my java code.

Os this possible?
if yes, I'm thinking something like this, from my java code

  • load the .xsd file
  • generate the .xml
  • save the .xml file

Can someone direct me to a good resource and or tell me if my thinking is wrong

I've had good experiences using XMLBeans , however I've always had the XSD available at compile time. It integrates nicely with Maven (plus potentially other build systems). The compilation produces a series of Java classes that can be used to construct an XML document that conforms to the XSD or process an XML file you've received.

You can potentially do some runtime processing of an XSD using the org.apache.xmlbeans.XmlBeans.compileXsd class, but I've never experimented with it. Just seen a reference from an FAQ.

I think the main problem is that to do it in a clean way you should have classes reflecting your xsd. Xsd defines a data model, so the important part is to recreate it with classes. If you want to do it dynamically it could be rather difficult. If you want to do it at compile time- jaxb is the way to go. There is very interesting article talking about problems related with parsing xml (it goes from a different perspective than you describe), but I think there is a wealth of knowledge to be learned from here:

http://elegantcode.com/2010/08/07/dont-parse-that-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