我正在尝试解压缩一个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>
每当我试图用这个功能做unmarshall时:
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包含一个无效字符<。
===================EXCEPTION THROWN======================
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无效。
还有什么是我需要做的或者是我错过的配置吗?我该怎么解决这个问题?
发布于 2016-08-24 09:25:11
我得从某种程度上解决。
我所做的是转义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进行解组将为适当的对象提供属性值。
https://stackoverflow.com/questions/36425093
复制相似问题