JAXB - 覆盖元素的数据类型

标签 jaxb

我将 JAXB 与以下自定义绑定(bind)一起使用:

<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" version="2.0" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" jaxb:extensionBindingPrefixes="xjc" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <jaxb:globalBindings typesafeEnumMaxMembers="2000">
        <jaxb:serializable />
    </jaxb:globalBindings>
        <jaxb:bindings schemaLocation="test.xsd" node="/xs:schema//xs:complexType[@name='EN']//xs:element[@name='family']">
            <jaxb:property>
                <jaxb:baseType name="java.lang.String" />
            </jaxb:property>
        </jaxb:bindings>
</jaxb:bindings>

处理这个模式(test.xsd):
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
   <xsd:complexType name="EN" mixed="true">
      <xsd:complexContent>
         <xsd:extension base="ANY">
            <xsd:sequence>
               <xsd:choice minOccurs="0" maxOccurs="unbounded">
                  <xsd:element name="family" type="en.family"/>
               </xsd:choice>
            </xsd:sequence>
         </xsd:extension>
      </xsd:complexContent>
   </xsd:complexType>
   <xsd:complexType name="en.family" mixed="true">
      <xsd:complexContent>
         <xsd:restriction base="ENXP">
         </xsd:restriction>
      </xsd:complexContent>
   </xsd:complexType>
   <xsd:complexType name="ENXP" mixed="true">
      <xsd:complexContent>
         <xsd:extension base="ST">
         </xsd:extension>
      </xsd:complexContent>
   </xsd:complexType>
   <xsd:complexType name="ST" mixed="true">
      <xsd:complexContent>
         <xsd:restriction base="ED">
         </xsd:restriction>
      </xsd:complexContent>
   </xsd:complexType>
   <xsd:complexType name="ED" mixed="true">
      <xsd:complexContent>
         <xsd:extension base="BIN">
         </xsd:extension>
      </xsd:complexContent>
   </xsd:complexType>
   <xsd:complexType name="BIN" abstract="true" mixed="true">
      <xsd:complexContent>
         <xsd:extension base="ANY">
         </xsd:extension>
      </xsd:complexContent>
   </xsd:complexType>
   <xsd:complexType name="ANY" abstract="true">
      <xsd:attribute name="nullFlavor" type="xsd:string" use="optional">
      </xsd:attribute>
   </xsd:complexType>
</xsd:schema>

我遇到了这个问题:
[ERROR] compiler was unable to honor this property customization. It is attached to a wrong place, or its inconsistent with other bindings.
  line 6 of file://home/user/test/jaxbelem_binding.xml

[ERROR] (the above customization is attached to the following location in the schema)
  line 7 of file://home/user/test/test.xsd

Failed to parse a schema.

我的绑定(bind)文件有什么问题?
关于jaxb:property ,但是当后者被删除时,不会发生覆盖!

任何帮助将不胜感激,在此先感谢各位!

最佳答案

我很幸运嵌套了 jxb:bindings node jxb:bindings schemaLocation 内的标签像这样的标签:

<?xml version="1.0" encoding="US-ASCII" ?>
<jxb:bindings version="2.0" 
    xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
    xmlns:jxb="http://java.sun.com/xml/ns/jaxb" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
>

    <jxb:bindings schemaLocation="test.xsd"> 
        <jxb:bindings node="//xs:element[@name='family']">
            <jxb:property >
                <jxb:baseType name="java.lang.String"></jxb:baseType>
            </jxb:property>
        </jxb:bindings>
    </jxb:bindings>
</jxb:bindings>

还:
  • 虽然您对“家庭”的注释 xpath 有效,但它似乎过于复杂。根据您的示例 xsd,我将其更新为可以使用的一种(只要只有一个 name='family' 的元素就可以使用)
  • 关于JAXB - 覆盖元素的数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13633231/

    相关文章:

    java - 如何防止在 CXF Web 服务客户端中生成 JAXBElement<String>?

    java - 解析 Web 服务响应时出现问题 (Metro/JAXB)

    java - 困难的 JAXB 映射

    java - 无法将 XSD 转换为 Java 类

    c# - jaxb marshal 生成空文件

    java - JAXB - XML 输出中的意外元素

    java - 标签的 JAXB 解码

    java - 为什么 JAXB 试图绑定(bind) Spring 的 ApplicationContext?

    java - 使用 JAXB 将 HashMap<String, List<String>> 转换为 XML

    java - 从许多属性中,我需要获取其中之一并使用 jaxb 在 xml 字符串中打印