我正在寻找以下问题的解决方案。
我有如下所示的 xml 内容:
<my:Chapter>
<my:Number>8.1.</my:Number>
<my:Title>chapter title</my:Title>
<my:Text>
<div xmlns="http://www.w3.org/1999/xhtml">dsfsdfsda sadfa ef aw</div>
<div xmlns="http://www.w3.org/1999/xhtml">aawfwa ef</div>
<div xmlns="http://www.w3.org/1999/xhtml">aw</div>
</my:Text>
</my:Chapter>
节点“文本”可以包含任何 xhtml 元素。当将此 xml 编组为 java 类时,我需要此文本内容,因为它是一个字符串。
该元素的 XSD 如下所示:
<xsd:element name="Text">
<xsd:complexType mixed="true">
<xsd:sequence>
<xsd:any minOccurs="0" maxOccurs="unbounded" namespace="http://www.w3.org/1999/xhtml"
processContents="lax"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
JAXB 类生成生成以下内容:
@XmlRootElement(name = "Chapter")
public class Chapter {
@XmlElement(name = "Number")
protected String number;
@XmlElement(name = "Title")
protected String title;
@XmlElement(name = "Text")
protected Text text;
getters and setters ...
}
@XmlRootElement(name = "Text")
public class Text {
@XmlMixed
@XmlAnyElement(lax = true)
protected List<Object> content;
public List<Object> getContent() {
if (content == null) {
content = new ArrayList<Object>();
}
return this.content;
}
}
目前 Text 元素的所有子元素都编组为List<Object>
,需要再次解组才能将 xhtml 内容作为字符串。在我看来,这是浪费处理时间。
我想要的是,xhtml 文本内容只是作为一个字符串。像这样:
@XmlRootElement(name = "Chapter")
public class Chapter {
@XmlElement(name = "Number")
protected String number;
@XmlElement(name = "Title")
protected String title;
@XmlElement(name = "Text")
protected String text;
getters and setters ...
}
我已经为其他一些情况设置了一些 xjb 绑定和 XmlAdapter 并期望这也可以作为解决方案,但到目前为止我没有成功实现我想要的。
XML 文件无法更改,因为它已经是生产数据。XSD 和绑定在我的控制之下,并且可以更改。
有人有想法吗?