gpt4 book ai didi

用于询问 XSD 文件的 Java 方法

转载 作者:数据小太阳 更新时间:2023-10-29 01:56:44 25 4
gpt4 key购买 nike

我有一组用于不同数据类型的 xsd 文件。在 Java 世界中,生成类型属性列表的最佳方法是什么?

例如用这两个文件。

文件:customer.xsd

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="customer">
<xs:complexType>
<xs:sequence>
<xs:element name="number" type="xs:integer"/>
<xs:element name="name" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

文件:order.xsd

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="customer">
<xs:complexType>
<xs:sequence>
<xs:element name="orderid" type="xs:integer"/>
<xs:element name="customer" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

我想做两件事

1. 一个 Java 应用程序,它读取 XSD 并随后进行处理(以某种方式?)。因此,当您运行该程序时,它可以打印出属性

> java -jar printtypes.jar -f customer.xsd
> number : Integer
> name : String
> address : String

2.某种生成新文件的转换

文件:customer.properties

<propertylist>
<prop>
<name> orderid </name>
<type> integer </type>
</prop>
<prop>
<name> customer </name>
<type> string</type>
</prop>
</propertylist>

我尝试使用 java 反射来查询 JAXB 生成的 Java 类来实现上面 (1) 中的程序。这创建了该类的一个实例并询问了值和字段,但它在值由空序列组成的情况下不起作用。由于类型删除,无法恢复到值的原始类型。你最终得到了一个空的 ArrayList,但你不知道是什么。

我从事 C++ 工作,所以目前我对所有这些 Java 技术有点迷茫。我的 Google 能力让我失望了——我看到的大多数 JAVA/XSD 帖子都在谈论验证,这不是我想要做的。

最佳答案

您可能想查看 XSOM,它是一个将摄取您的 XML 架构并生成您可以遍历并生成所需结果的对象的项目。

http://xsom.java.net/userguide.html

手动解析模式可能非常棘手,因为可能有不同的方式来表达基本相同的内容。

关于用于询问 XSD 文件的 Java 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7940931/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com