【发布时间】:2012-08-19 12:11:44
【问题描述】:
我正在使用 JAXB 生成 OpenOffice XML,并且需要将 xml:space="preserve" 属性应用于 t 元素。
<r>
<t>
foo
</t>
</r>
应该是
<r>
<t xml:space="preserve">
foo
</t>
</r>
我使用 JAXB 从 Open Office 模式生成 Java 类。 <t> 类型在 CTRElt Java 类中表示为 String,因此无法设置该属性。当我从现有文档中解组并编组时,以前存在的 space 属性不再存在。
架构的相关部分如下:
<xsd:element name="t" type="ST_Xstring" minOccurs="1" maxOccurs="1">
<xsd:annotation>
<xsd:documentation>Text</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:simpleType name="ST_Xstring">
<xsd:annotation>
<xsd:documentation>Escaped String</xsd:documentation>
</xsd:annotation>
<xsd:restriction base="xsd:string" />
</xsd:simpleType>
如何生成具有此必需 space 属性的 XML?修改架构不是一种选择。我需要自定义 JAXB 吗?
【问题讨论】:
-
您可能会使用
XmlAdapter来处理此用例。以下文章可能会有所帮助:blog.bdoughan.com/2011/08/xml-schema-to-java-generating.html -
@BlaiseDoughan,这可以让您格式化标签中的值,但我看不到通过添加属性来自定义标签元素本身的方法。