代码之家  ›  专栏  ›  技术社区  ›  kugelblitz

XMLBeans 5.1.0的问题:无法生成具有具体类型的XML元素

  •  0
  • kugelblitz  · 技术社区  · 2 年前

    我目前正在进行一个项目,需要使用XMLBeans 5.1.0生成具有具体类型的XML元素。然而,我在实现这一目标方面遇到了困难。

    我有一个XSD模式,它定义了一个抽象元素ChartcValue和几个使用替换组从抽象元素派生的具体元素(BooleanCharistic、DateCharacteristic、IntegerCharistic等)。我已经在XSD模式中定义了必要的复杂类型和替换组。

    以下是我的XSD片段:

    请求.xsd

    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:chartc="http://<>/characteristicvalues/v17_2">
    
      <xs:element name="Request">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="ID" type="xs:int" />
            <xs:element name="CharacteristicValue" type="CharacteristicValueType" minOccurs="0" />
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:schema>
    
    

    CharacteristicValues.xsd

    <xs:element name="ChartcValue" abstract="true">
      <xs:annotation>
        <xs:documentation xml:lang="en">
          Element that is substituted by a characteristic value.
        </xs:documentation>
      </xs:annotation>
    </xs:element>
    
    <xs:element name="TextCharacteristic" type="TextCharacteristicType" substitutionGroup="ChartcValue">
      <xs:annotation>
        <xs:documentation>
          Specification for a Text Characteristic.
        </xs:documentation>
      </xs:annotation>
    </xs:element>
    

    Java类是成功生成的,但当我尝试以编程方式创建具体类型的实例并生成XML元素时,它只允许我创建抽象ChartcValue类型的实例,而不允许创建从它派生的具体类型。

    下面是我用来生成XML元素的代码示例:

    CharacteristicValueType charValue = CharacteristicValueType.Factory.newInstance();
    XmlObject obj = charValue.addNewChartcValue();
    TextCharacteristicType textCharacteristic = TextCharacteristicType.Factory.newInstance();
    textCharacteristic.setShortName(shortName);
    textCharacteristic.setStringValue(value);
    obj.set(textCharacteristic);
    
    

    对于如何使用XMLBeans 5.1.0正确生成具有具体类型的XML元素,我将非常感谢任何指导或建议。在我的模式中或在生成过程中,是否缺少某些东西?我们将高度赞赏任何见解或替代方法来实现这一目标。

    0 回复  |  直到 2 年前