Creating XML file from .xsd using java

I am completely new to the java world and I have a requirement to generate an XML file from a .xsd file.

I did some research and found that "jaxb" can do this. And I found an example, but the problem is that almost all examples use the "xjc" tool to do this. But I want to do this through my Java code.

Leave it possible?
if yes, I think something like this from my java code

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

Can someone direct me to a good resource and / or say if I am mistaken in error

+4
source share
2 answers

I had a good experience using XMLBeans , however I always had XSD available at compile time. It goes well with Maven (plus potentially other build systems). Compilation creates a series of Java classes that can be used to build an XML document that matches the XSD or processes the resulting XML file.

You can do some XSD processing at runtime using the org.apache.xmlbeans.XmlBeans.compileXsd class, but I have never experimented with it. Just looked through the link from the FAQ.

0
source

I think the main problem is that for pure use you should have classes reflecting your xsd. Xsd defines a data model, so the important part is to recreate it using classes. If you want to do it dynamically, it can be quite complicated. If you want to do this at compile time - jaxb is the way to go. There is a very interesting article about the problems associated with parsing xml (it comes from a different perspective than you describe), but I think there is a lot of knowledge here:

http://elegantcode.com/2010/08/07/dont-parse-that-xml/

0
source

Source: https://habr.com/ru/post/1439765/


All Articles