我正在从事一个涉及 Java 后端和 Delphi 前端的项目。我正在尝试基于 Java 中的 .xsd 生成 XML 绑定(bind)。 XSD 包含一个名为 TaskList 的对象,其中包含项目 Tasks。任务是任务列表。当我生成 XML 绑定(bind)时,Delphi 尝试使用 TXMLTaskList 的 CreateCollection() 函数但抛出错误,因为 TXMLTaskList 是 IXMLNode 而不是 IXMLNodeCollection。
我仍然不熟悉使用 XSD 文件和 XML 绑定(bind)生成功能,但基于我所了解的一点点我假设因为 TaskList 包含一个对象任务,它不应该在 CreateCollection 函数中使用,而是我会认为应该使用作为任务列表的任务。
这是我的 XML 绑定(bind)文件抛出错误的行:
FExportOnClientChange := CreateCollection(TXMLTaskList, IXMLTask,
'exportOnClientChange') as IXMLTaskList;
这是我的 TXMLTaskLisk,表明它是一个 TXMLNode 而不是 CreateCollection 正在寻找的 TXMLNodeCollectionClass。
type
TXMLTaskList = class(TXMLNode, IXMLTaskList)
protected
{ IXMLTaskList }
function Get_Tasks: IXMLTasks;
public
procedure AfterConstruction; override;
end;
在我试图找出问题的过程中,我确实注意到,如果我将 TaskList 设为一个无限任务列表,并将任务保留为一个无限任务列表,那么在 delphi xml 文件中一切都会正常生成,但这意味着我有一个列表的列表,这不是我想要的。
这里可能很难说的一件事是 TaskList 和任务位于不同的 XSD 文件中,尽管它们是链接的。
<complexType name="TaskList">
<sequence>
<element name="tasks" type="struct:tasks"></element>
</sequence>
</complexType>
<complexType name="tasks">
<sequence>
<element ref="struct:task" maxOccurs="unbounded" minOccurs="0"></element>
</sequence>
</complexType>
最佳答案
您在 Delphi 部门是一个人。
话虽如此,我确实使用 xjc 和 xsd 文件来生成 Java 类。
看起来您在问题中包含了一个您知道是错误的 xsd 样本。在您的 xsd 示例中,您有一系列序列 - 我希望您的示例生成一个包含任务对象集合的类。
如果我正确理解您的问题,您希望 TaskList 包含任务集合。那应该不会太难。你试过什么?
这是我如何生成包含单个 Threshold 对象列表的 Thresholds 对象的示例:
<xsd:element name="Thresholds" type="ThresholdsType"/>
<xsd:complexType name ="ThresholdsType">
<xsd:sequence>
<xsd:element ref="Threshold" maxOccurs="unbounded" minOccurs="0"/>
</xsd:sequence>
<xsd:attribute name="interpolate" type="xsd:string" use="optional" />
<xsd:attribute name="parameter" type="xsd:string" use="optional" />
<xsd:attribute name="unitSystem" type="xsd:string" use="optional" />
</xsd:complexType>
<xsd:element name="Threshold" type="ThresholdType"/>
<xsd:complexType name="ThresholdType">
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute type="xsd:string" name="id" use="optional"/>
<xsd:attribute type="xsd:double" name="minInclusive" use="optional"/>
<xsd:attribute type="xsd:double" name="maxExclusive" use="optional"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
这里是生成的 ThresholdsType java 类的开始:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ThresholdsType", propOrder = {
"threshold"
})
@javax.xml.bind.annotation.XmlRootElement(name="ThresholdsType implements Cloneable, Named, Visitable, CopyTo, Equals, HashCode, ToString")
public class ThresholdsType implements Cloneable, Named, Visitable, CopyTo, Equals, HashCode, ToString
{
@XmlElement(name = "Threshold")
protected List<ThresholdType> threshold;
@XmlAttribute(name = "interpolate")
protected String interpolate;
@XmlAttribute(name = "parameter")
protected String parameter;
@XmlAttribute(name = "unitSystem")
protected String unitSystem;
@XmlTransient
private QName jaxbElementName;
...
我应该将 ThresholdsType 中的“threshold”字段重命名为“thresholds”,但除此之外效果很好。
这行不通吗?
<complexType name="TaskList">
<sequence>
<element ref="struct:task" maxOccurs="unbounded" minOccurs="0"/>
</sequence>
</complexType>
关于java - XML 绑定(bind)生成文件不编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29905096/