BEANIO CDATA issue with woodstox as default xml parser

116 views
Skip to first unread message

Ahmed Hussein

unread,
Jan 26, 2017, 5:03:30 AM1/26/17
to beanio-users
Hallo Kevin,

we have a issue with reading XML-File with a CDATA element. 
We use Woodstox jar as  a default parser for xml.

with this constellation BEAN IO ignores the CDATA Element, so i tried at first to override the systemproperty 


Properties props = System.getProperties();
props.setProperty("javax.xml.stream.XMLInputFactory", "com.sun.xml.internal.stream.XMLInputFactoryImpl");

which worked good, but we can't use this solution on productive environments. (e.g. IBM don't use sun mircorsystems implementations ...)

so i debugged the source code of BEAN IO and "woodstox-core-asl-4.4.1", and found out, that woodstox looks for the COALESCING property to be able to read CDATA-element well.

so i added this  line in the constructor to both classes "org.beanio.stream.xml.XmlReader" and "org.beanio.stream.xml.XmlWriter" as well

XmlReader.xmlInputFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);


Now it reads CDATA and there ist no problems.

can you please test this issue and give me your feedback if possible.


Best regards
Ahmed

Frits Jalvingh

unread,
Apr 10, 2017, 4:58:39 AM4/10/17
to beanio-users
Hi Ahmed,

Thanks for the detailed description, I had the same problem and was able to fix it quickly because of your work!

Just for reference: I did not want to patch the source for BeanIO, so instead I added the following code to "patch" BeanIO at runtime:

    static {
        Class<XmlReader> xmlReaderClass = XmlReader.class;
        try {
            Field factoryField = xmlReaderClass.getDeclaredField("xmlInputFactory");
            factoryField.setAccessible(true);
            Object o = factoryField.get(null);                  // Get the factory
            XMLInputFactory factory = (XMLInputFactory) o;
            factory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
            System.out.println("QD: Patched BEANIO to use COALESCING character data.");
        } catch (Exception x) {
            System.err.println("==========================================================");
            System.err.println("FAILED TO PATCH BEANIO - " + x);
            System.err.println("==========================================================");
        }
    }

This uses Reflection to fix the issue. Add the code to some class as a static initializer, or call it once at init time.

Regards,

Frits
Reply all
Reply to author
Forward
0 new messages