【发布时间】:2016-04-05 11:28:18
【问题描述】:
我正在尝试解组如下所示的 XML:
===============================[XML]============== =======================
<Element1>
<innerElement attr1="value1">
<ConcernedElement FirstAttribute="FirstValue" SecondAttribute="<![CDATA[<AttributeElement aAttribute="aValue" bAttribute="bValue"><vElement vAttrib="aV.Value"></vElement></AttributeElement>]]>"></ConcernedElement>
</innerElement>
</Element1>
Schema 定义如下:
================================[XSD]============== ====================
<xs:element name="Element1">
<xs:complexType>
<xs:sequence>
<xs:element ref="innerElement" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="innerElement">
<xs:complexType>
<xs:sequence>
<xs:element ref="ConcernedElement" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="ConcernedElement">
<xs:complexType>
<xs:attribute name="FirstAttribute" type="xs:string"/>
<xs:attribute name="SecondAttribute" type="xs:string"/>
</xs:complexType>
</xs:element>
每当我尝试使用此功能解组时:
public Object unmarshall(String xml) {
try {
StringBuffer stringBuffer = new StringBuffer(xml);
StringReader stringReader = new StringReader(stringBuffer.toString());
StreamSource streamSource = new StreamSource(stringReader);
Object object = customUnmarshaller.unmarshal(streamSource);
return object;
} catch (Exception ex) {
return null;
}
}
我得到一个异常 SecondAttribute 包含无效字符 <.>
===================[抛出异常]======================
ex = (org.springframework.oxm.UnmarshallingFailureException) org.springframework.oxm.UnmarshallingFailureException: JAXB unmarshalling exception; nested exception is javax.xml.bind.UnmarshalException
- with linked exception:
[org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 159; The value of attribute "SecondAttribute" must not contain the '<' character.]
此外,执行 XML 验证表明 XML 无效。
还有什么我需要做的或者我遗漏的配置吗? 我该如何解决这个问题?
【问题讨论】:
-
你解决了吗?我遇到了同样的问题。
-
我所做的是转义 SecondAttribute 的值并用转义值替换 XML 字符串中的原始值...这样,Marshaller 能够解组整个 XML 并检索到 SecondAttribute也正确。
标签: java xml spring-mvc jaxb unmarshalling