Java 使用JAXB2 OXM解组包含CDATA属性的XML
我正在尝试解组一个如下所示的XML: ====================================================[XML]====================================Java 使用JAXB2 OXM解组包含CDATA属性的XML,java,xml,spring-mvc,jaxb,unmarshalling,Java,Xml,Spring Mvc,Jaxb,Unmarshalling,我正在尝试解组一个如下所示的XML: ====================================================[XML]==================================== <Element1> <innerElement attr1="value1"> <ConcernedElement FirstAttribute="FirstValue" SecondAttribute="<![CDATA[&l
<Element1>
<innerElement attr1="value1">
<ConcernedElement FirstAttribute="FirstValue" SecondAttribute="<![CDATA[<AttributeElement aAttribute="aValue" bAttribute="bValue"><vElement vAttrib="aV.Value"></vElement></AttributeElement>]]>"></ConcernedElement>
</innerElement>
</Element1>
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.]
我得到一个异常,SecondAttribute包含一个无效字符,我需要以某种方式解决
我所做的是转义SecondAttribute的值,并用转义值替换XML字符串中的原始值。。。这样,封送拆收器也能够正确地解组整个XML和检索到的第二个属性
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将为相应的对象提供属性值。您解决了吗?我遇到了同样的问题。我所做的是转义SecondAttribute的值,并用转义值替换XML字符串中的原始值。。。这样,封送拆收器也能够正确地解组整个XML和检索到的第二个属性。
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.]
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);
}