使用JAXB2 OXM

时间:2016-04-05 11:28:19

标签: java xml spring-mvc jaxb unmarshalling

我正在尝试解组一个看起来像这样的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 包含无效字符&lt;。

=================== [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无效。

我还有其他需要做的事情或我错过的配置吗? 我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

我必须在某种程度上解决。

我所做的是逃避 SecondAttribute 的值,并用转义值替换XML String中的原始值......这样,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将为适当的Object提供属性&#39;值。