4

I want to create XSD from xml document using java code.I tried with different solution using XsdGen & Xbean jar,but it won't work.You can referred this also:http://www.codesuggestions.com/java/how-to-create-xsd-from-xml-using-java-application/. My task is able to generate XSD from XML and If I changed that xml then another xsd also get append or merge with previous one xsd .Let say, I have xml1,xml2...xml5 document file, for all these xml documents generate only one XSD.

Condition: Suppose, I have xml1.xml file i.e.

   <?xml version="1.0" ?>
   <Student>
        <RollNo>123</RollNo>
        <Name>xyz</Name>
   </student>

For this xml, It will be generate Student.xsd and If I change previous xml i.e.xml1.xml then It should verified that Student.xsd available or not If available then make changes in that XSD otherwise append new XSD to old XSD.

Thanks.

Akshay Kamble
  • 59
  • 1
  • 1
  • 6
  • It is generally not possible to generate a useful XSD from an XML document. This would be like trying to generate the entire Java grammar from examining a single program. The XSD specifies rules to validate ALL possible XML files according to a schema. Starting from one XML file it is not possible to deduce the schema. You can make a few guesses but the result will not be what you want. – Jim Garrison Nov 28 '17 at 07:03
  • There are some online editor who generate/create xsd automatically,We can also create but How?that I don't know.If you have any idea then please share that help me lot.Thanks @JimGarrison. – Akshay Kamble Nov 28 '17 at 07:13
  • What they create will of necessity apply only to XML that looks substantially like the XML they were provided. There's no guarantee that the XSD will work for other XML that you consider valid but which contained sequences or structures that were not present in the sample. Or, the constraints deduced by the tool will be too lax for your actual requirements. Expect to have to manually modify whatever you get. – Jim Garrison Nov 28 '17 at 07:17

2 Answers2

2

There are tools to create an XSD from an XML instance (or a set of instances) but they will never create an XSD that is suitable for production use - it will always need manual tweaking. Therefore, this is not something you would normally do from a Java application.

The reason is that the tools have to make guesses. Just because all the prices in your sample data are more than $10.00, and all the book titles consist entirely of ASCII letters and spaces, doesn't mean that this will also apply to the prices and titles in the next document you want to process. Equally, if every book element in your sample has exactly one author, that doesn't stop the next sample containing a book with no author or several authors.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
2

You can use Intellij Ultimate edition to create and XSD from XML.it is an ALL in one ide which acan be used for several purposes.

create XML file go to tools> XML tools> generate XSD file.