读取 XSD 文件以获取 Java 中的所有元素和相应属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18651582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading an XSD file to get all the elements and corresponding attributes in Java
提问by Aditya
My requirement is to read an XSD file and to get all its elements with corresponding attributes in java. I've been trying to convert my XSD file to a sample XML file as parsing an XML file to get all it's elements and attributes is easy. But so far I've been unable to find a good enough tool in java to programmatically convert my XSD to sample XML.
我的要求是读取一个 XSD 文件并在 java 中获取其所有具有相应属性的元素。我一直在尝试将我的 XSD 文件转换为示例 XML 文件,因为解析 XML 文件以获取它的所有元素和属性很容易。但到目前为止,我一直无法在 java 中找到足够好的工具来以编程方式将我的 XSD 转换为示例 XML。
Is there any free and good java code available to convert an XSD to its sample XML?
是否有任何免费且优秀的 Java 代码可用于将 XSD 转换为其示例 XML?
Or else what is the way I can read all the elements and attributes that a sample XML would contain directly from the XSD file?
或者,我可以通过什么方式直接从 XSD 文件中读取示例 XML 将包含的所有元素和属性?
Thanks in advance!
提前致谢!
回答by Juned Ahsan
The xsd format is perfectly valid XML, so you can parse an XML schema file with any xml parser.
xsd 格式是完全有效的 XML,因此您可以使用任何 xml 解析器解析 XML 模式文件。
Check this related post to get some code samples and ideas:
查看此相关帖子以获取一些代码示例和想法:
回答by Michael Kay
Processing a raw XSD document as XML is quite tricky except in very simple cases.
除了非常简单的情况外,将原始 XSD 文档作为 XML 处理非常棘手。
Alternatives are:
替代方案是:
(a) use an API for accessing a compiled schema (for example, there is such an API in Xerces)
(a) 使用 API 来访问已编译的模式(例如,Xerces 中有这样的 API)
(b) Saxon's schema processor can output an XML representation of the compiled schema, which is much easier to process than the raw XSD documents (for example, it combines everything into one document, and presents the relationship of elements to types in a uniform way).
(b) Saxon 的模式处理器可以输出编译后模式的 XML 表示,这比原始 XSD 文档更容易处理(例如,它将所有内容合并为一个文档,并以统一的方式呈现元素与类型的关系)。
回答by user2630745
Not sure if this solves the problem. I have similar requirement of accessing elements and attributes of xsd. Using Eclipse, the solution to create XML out of xsd is quite easy. I have a dynamic web project created and put my xsd in webcontent. Right click on XSD and there is option called 'Generate'. It shows 2 options to generate XML or JAXB classes. On clicking Generate XML, XML file is created from XSD. I hope you were looking for this solution.
不确定这是否能解决问题。我对访问 xsd 的元素和属性有类似的要求。使用 Eclipse,从 xsd 创建 XML 的解决方案非常简单。我创建了一个动态 Web 项目并将我的 xsd 放在 webcontent 中。右键单击 XSD,有一个名为“生成”的选项。它显示了生成 XML 或 JAXB 类的 2 个选项。单击生成 XML 后,将从 XSD 创建 XML 文件。我希望你正在寻找这个解决方案。
回答by Adnan Sadiq
There is a tool in java to convert my XSD to sample XML. You need to add jaxb-xjc (executable jar file) in the build path of your project.Once you are done with it just put the .xsd file in src, right click on it and find 'Generate' option, you can now find XML and Jaxb classes against 'Generate' option, Select Xml and get your XML file generated from .xsd.
java中有一个工具可以将我的XSD转换为示例XML。您需要在项目的构建路径中添加 jaxb-xjc(可执行 jar 文件)。完成后只需将 .xsd 文件放在 src 中,右键单击它并找到“生成”选项,您现在可以找到XML 和 Jaxb 类针对“生成”选项,选择 Xml 并从 .xsd 生成您的 XML 文件。