首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用JAXB2 OXM随属性包含CDATA的Unmarshall

使用JAXB2 OXM随属性包含CDATA的Unmarshall
EN

Stack Overflow用户
提问于 2016-04-05 11:28:18
回答 1查看 116关注 0票数 0

我正在尝试解压缩一个XML,它如下所示:

==============================XML====================================

代码语言:javascript
复制
<Element1>
<innerElement attr1="value1">
    <ConcernedElement FirstAttribute="FirstValue" SecondAttribute="<![CDATA[<AttributeElement aAttribute="aValue" bAttribute="bValue"><vElement vAttrib="aV.Value"></vElement></AttributeElement>]]>"></ConcernedElement>
</innerElement>
</Element1>

架构定义如下:

==============================XSD=================================

代码语言:javascript
复制
<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时:

代码语言:javascript
复制
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======================

代码语言:javascript
复制
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无效。

还有什么是我需要做的或者是我错过的配置吗?我该怎么解决这个问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-24 09:25:11

我得从某种程度上解决。

我所做的是转义SecondAttribute的值,并用转义值替换XML中的原始值.这样,Marshaller就能够对整个XML和SecondAttribute进行正确检索。

代码语言:javascript
复制
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进行解组将为适当的对象提供属性值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36425093

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档