I used code very similar to the code in Blaze's answer, and recently discovered that he had a subtle problem. The XMLReader that I received from SAXParser was not configured to understand namespaces, which meant that it did not properly handle elements such as
<myType> <myIntegerElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> </myType>
Instead of JAXB unmarshalling myIntegerElement for null Integer in Java, it was decoded to Integer.valueOf(0) ; important difference for my code. The solution was to set the factory parser as a namespace:
SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); spf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); SAXParser sp = spf.newSAXParser(); // and so on
I found this rather unexpected, because if I got my XMLReader by executing XMLReaderFactory.createXMLReader() , then the reader had no problems with nillable elements, but he also did not understand XMLConstants.FEATURE_SECURE_PROCESSING.
source share