使用java将xml转换为xsd

时间:2022-05-07 23:03:52

I am looking for a tool or java code or class library/API that can generate XSD from XML files. (Something like the xsd.exe utility in the .NET Framework sdk)

我正在寻找可以从XML文件生成XSD的工具或java代码或类库/ API。 (类似于.NET Framework sdk中的xsd.exe实用程序)

4 个解决方案

#1


These tools can provide a good starting point, but they aren't a substitute for thinking through what the actual schema constraints ought to be. You get the opportunity for two kinds of errors: (1) allowing XML that shouldn't be allowed and (2) disallowing XML that should be ok.

这些工具可以提供一个良好的起点,但它们不能代替思考实际的模式约束应该是什么。您有机会遇到两种错误:(1)允许不允许的XML和(2)禁止应该没问题的XML。

As an example, pretend that you want to infer an XSD from a few thousand patient records that include a 'gender' tag (I used to work on medical records software). The tool would likely encounter 'M' and 'F' as values and might deduce that the element is an enumeration. However, other valid (although rarely used) values are B (both), U (unknown), or N (none). These are rare, of course. So, if you used your derived schema as an input validator, it would perform well until a patient with multiple sex organs was admitted to the hospital.

例如,假装您想要从包含“性别”标签的几千个患者记录中推断XSD(我曾经在医疗记录软件上工作)。该工具可能会遇到'M'和'F'作为值,并可能推断该元素是枚举。但是,其他有效(尽管很少使用)值是B(两者),U(未知)或N(无)。当然,这些很少见。因此,如果您使用衍生模式作为输入验证器,那么在患有多个性器官的患者被送入医院之前,它将表现良好。

Conversely, to avoid this error, an XSD generator might not add enumerated type restrictions (I can't remember what these are called in schemas), and your application would work well until it encountered an errant record with gender=X.

相反,为了避免这个错误,XSD生成器可能不会添加枚举类型限制(我不记得在模式中调用这些限制),并且您的应用程序将运行良好,直到它遇到性别= X的错误记录。

So, beware. It's best to use these tools only as a starting point. Also, they tend to produce verbose and redundant schemas because they can't figure out patterns as well as humans.

所以,要小心。最好只使用这些工具作为起点。而且,它们倾向于产生冗长和冗余的模式,因为它们不能像人类那样找出模式。

#2


Check Castor, I think it has the functionality you are looking for. They also provide you with an ant task that creates XSD schemas from XML files.

检查Castor,我认为它具有您正在寻找的功能。它们还为您提供了一个从XML文件创建XSD架构的ant任务。

PS I suggest you to add more specific tags in the future: For instance, using xml, xsd and java will increment the possibility of getting answers.

PS我建议你将来添加更多特定的标签:例如,使用xml,xsd和java将增加获得答案的可能性。

#3


You can use xsd-gen-0.2.0-jar-with-dependencies.jar file to convert xml to xsd. And Command for it is "java -jar xsd-gen-VERSION-jar-with-dependencies.jar /path/to/xml.xml > /path/to/my.xsd"

您可以使用xsd-gen-0.2.0-jar-with-dependencies.jar文件将xml转换为xsd。它的命令是“java -jar xsd-gen-VERSION-jar-with-dependencies.jar /path/to/xml.xml> /path/to/my.xsd”

#4


Try the xsd-gen project from Google.

试试Google的xsd-gen项目。

https://code.google.com/p/xsd-gen/

#1


These tools can provide a good starting point, but they aren't a substitute for thinking through what the actual schema constraints ought to be. You get the opportunity for two kinds of errors: (1) allowing XML that shouldn't be allowed and (2) disallowing XML that should be ok.

这些工具可以提供一个良好的起点,但它们不能代替思考实际的模式约束应该是什么。您有机会遇到两种错误:(1)允许不允许的XML和(2)禁止应该没问题的XML。

As an example, pretend that you want to infer an XSD from a few thousand patient records that include a 'gender' tag (I used to work on medical records software). The tool would likely encounter 'M' and 'F' as values and might deduce that the element is an enumeration. However, other valid (although rarely used) values are B (both), U (unknown), or N (none). These are rare, of course. So, if you used your derived schema as an input validator, it would perform well until a patient with multiple sex organs was admitted to the hospital.

例如,假装您想要从包含“性别”标签的几千个患者记录中推断XSD(我曾经在医疗记录软件上工作)。该工具可能会遇到'M'和'F'作为值,并可能推断该元素是枚举。但是,其他有效(尽管很少使用)值是B(两者),U(未知)或N(无)。当然,这些很少见。因此,如果您使用衍生模式作为输入验证器,那么在患有多个性器官的患者被送入医院之前,它将表现良好。

Conversely, to avoid this error, an XSD generator might not add enumerated type restrictions (I can't remember what these are called in schemas), and your application would work well until it encountered an errant record with gender=X.

相反,为了避免这个错误,XSD生成器可能不会添加枚举类型限制(我不记得在模式中调用这些限制),并且您的应用程序将运行良好,直到它遇到性别= X的错误记录。

So, beware. It's best to use these tools only as a starting point. Also, they tend to produce verbose and redundant schemas because they can't figure out patterns as well as humans.

所以,要小心。最好只使用这些工具作为起点。而且,它们倾向于产生冗长和冗余的模式,因为它们不能像人类那样找出模式。

#2


Check Castor, I think it has the functionality you are looking for. They also provide you with an ant task that creates XSD schemas from XML files.

检查Castor,我认为它具有您正在寻找的功能。它们还为您提供了一个从XML文件创建XSD架构的ant任务。

PS I suggest you to add more specific tags in the future: For instance, using xml, xsd and java will increment the possibility of getting answers.

PS我建议你将来添加更多特定的标签:例如,使用xml,xsd和java将增加获得答案的可能性。

#3


You can use xsd-gen-0.2.0-jar-with-dependencies.jar file to convert xml to xsd. And Command for it is "java -jar xsd-gen-VERSION-jar-with-dependencies.jar /path/to/xml.xml > /path/to/my.xsd"

您可以使用xsd-gen-0.2.0-jar-with-dependencies.jar文件将xml转换为xsd。它的命令是“java -jar xsd-gen-VERSION-jar-with-dependencies.jar /path/to/xml.xml> /path/to/my.xsd”

#4


Try the xsd-gen project from Google.

试试Google的xsd-gen项目。

https://code.google.com/p/xsd-gen/