FasterXML / jackson-dataformat-xml

Extension for Jackson JSON processor that adds support for serializing POJOs as XML (and deserializing from XML) as an alternative to JSON
Apache License 2.0
569 stars 221 forks source link

Issue while deserializing xml to POJO #401

Closed devendra-kumar1 closed 4 years ago

devendra-kumar1 commented 4 years ago

Below xml i am using this to extract CDATA value from a xml tag. but getting error.

XML <?xml version="1.1" ?>

Error "com.fasterxml.jackson.databind.JsonMappingException: Unexpected IOException (of type java.io.IOException): Expected END_ELEMENT, got event of type 1\r\n\tat com.fasterxml.jackson.databind.JsonMappingException.fromUnexpectedIOE(JsonMappingException.java:341)\r\n\tat com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3438)\r\n\tat com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3402)\r\n\tat com.poc.dynamodb.service.XmlConverterService.getConvertedXml(XmlConverterService.java:31)\r\n\tat com.poc.dynamodb.controller.XmlConverterController.getConvertedJson(XmlConverterController.java:20)\r\n\tat java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\r\n\tat java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)\r\n\tat java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\r\n\tat java.base/java.lang.reflect.Method.invoke(Method.java:566)\r\n\tat

Tried almost all help here but no luck. P.S. : I am using spring boot.

Any clue will be highly appreciated. Thanks in advance

cowtowncoder commented 4 years ago

Unfortunately this is not enough to help, as you included neither XML being read nor POJO you are trying to read data into. If you could include those it would be easier to suggest what might be going on. Also, please include Jackson version in use.

devendra-kumar1 commented 4 years ago

Ohh my Bad :( Below is the xml sample 👍

<?xml version="1.1" ?>
<MSG>
    <SYSHEADER></SYSHEADER>
    <APPDATA><![CDATA[      10001]]>
        <APPHEADER>
            <TOAPP/>
            <FROMAPP/>
        </APPHEADER>
    </APPDATA>
</MSG>

POJO :

public class MSG{
  private APPDATA appData;
}

public class APPDATA{
 String cdata;
}

I used lombok for all getter and setter and used DeserializationFeature.FAIL_ON_UNKNOW_PROPERTY to false while deserializing it. Version of jakson i used. 2.10.1

cowtowncoder commented 4 years ago

Thank you. One suggestion I would make for testing is to not disable DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTY as that can often hide actual problems (it often makes sense to use for production systems, for robustness, but less for testing).

The problem here, however, is that content model like this is not supported -- it is called "mixed data", and occurs when a nesting level has both elements and actual text (character data):

Basically xml like:

<APPDATA>some text cdata section or not makes no difference
   <APPHEADER> ... 
   </APPHEADER>
</APPDATA

can not really be bound by jackson-dataformat-xml at all currently.

Use of CDATA for section does not matter here.

It might be possible to allow ignoral of such mixed text values in future.

cowtowncoder commented 4 years ago

Created #402 to track improvements; will close this issue.