使用 JAXB2 OXM 解组 XML 属性包含 CDATA
Unmarshall XML with attribute containing CDATA using JAXB2 OXM
我正在尝试解组一个 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>
架构定义如下:
==============================[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 也可以正确检索。
public String getEscappedConcernedElementXML(String sourceXML) {
String concernedElementXMLString = findConcernedElementInXML(sourceXML);
if (concernedElementXMLString == null || (concernedElementXMLString.equal(""))) {
return concernedElementXMLString;
}
concernedElementXMLString = escapeSecondAttributeValueInXML(corcernedElementXMLString);
return concernedElementXMLString;
}
public String escapeSecondAttributeValueInXML(String sourceXML) {
String secondAttributeStartCursor = "SecondAttribute=\"";
int secondAttributeIndex = sourceXML.indexOf(secondAttributeStartCursor);
String secondAttributeEndCursor = "\">";
int secondAttributeEndIndex = sourceXML.indexOf(secondAttributeEndCursor, secondAttributeIndex);
String secondAttributeValue = sourceXML.substring(secondAttributeIndex + secondAttributeStartCursor.length(), secondAttributeEndIndex);
String escappedSecondAttributeValue = StringEscapeUtils.escapeXml(secondAttributeValue);
return sourceXML.replace(secondAttributeValue, escappedSecondAttributeValue);
}
然后,解组 XML 将为适当的对象提供属性值。
我正在尝试解组一个 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>
架构定义如下:
==============================[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 也可以正确检索。
public String getEscappedConcernedElementXML(String sourceXML) {
String concernedElementXMLString = findConcernedElementInXML(sourceXML);
if (concernedElementXMLString == null || (concernedElementXMLString.equal(""))) {
return concernedElementXMLString;
}
concernedElementXMLString = escapeSecondAttributeValueInXML(corcernedElementXMLString);
return concernedElementXMLString;
}
public String escapeSecondAttributeValueInXML(String sourceXML) {
String secondAttributeStartCursor = "SecondAttribute=\"";
int secondAttributeIndex = sourceXML.indexOf(secondAttributeStartCursor);
String secondAttributeEndCursor = "\">";
int secondAttributeEndIndex = sourceXML.indexOf(secondAttributeEndCursor, secondAttributeIndex);
String secondAttributeValue = sourceXML.substring(secondAttributeIndex + secondAttributeStartCursor.length(), secondAttributeEndIndex);
String escappedSecondAttributeValue = StringEscapeUtils.escapeXml(secondAttributeValue);
return sourceXML.replace(secondAttributeValue, escappedSecondAttributeValue);
}
然后,解组 XML 将为适当的对象提供属性值。