(De)Serialize mixed content with jackson xml?

无人久伴 提交于 2020-01-23 05:54:15

问题


With all the searching that I've done, I understand that serializing/deserializing xml with jackson that has mixed content is problematic. Does anybody know of a way to handle the following xml using Java?

<xmlsample>
    <title>Yada yada yada <a href=\"component:tcm:757-228001\" id=\"Link_1492103133595\" title=\"yada\" name=\"Link_1492103133595\" xmlns=\"xhtml\">yada</a> yada</title>
    <link>test</link>
</xmlsample>

I am using the following POJO:

@JacksonXmlRootElement(localName="xmlsample")
public class XmlSample{

    private String title;
    private String link;

    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getLink() {
        return link;
    }
    public void setLink(String link) {
        this.link = link;
    }
}

If the node has mixed content, as in the above example, I will get the following error:

java.io.IOException: Expected END_ELEMENT, got event of type 1

If the node has plain text, then deserialization works.

I have tried using JsonNode, TextNode, ObjectNode, Object instead of String for the data type. I have tried a custom serializer and deserializer, but the error persists. In fact, processing doesn't reach the custom deserializer if there is html in the node.

This xml is coming from a 3rd party system (SDL Tridion) that I cannot change.

Any help would be greatly appreciated!

EDIT: I need to clarify that the node could contain markup or could contain plain text, so I can't create a POJO that represents the markup as you see it in the above xml. And the markup in could be significantly more complex than the example above as well. This is why I am just trying to force it into a String. I don't need to manipulate it, I just need to preserve it in the POJO so it can be returned to the database unchanged.

来源:https://stackoverflow.com/questions/43531284/deserialize-mixed-content-with-jackson-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!