Bug 55864 - [PATCH] NullPointerException: XSSFImportFromXML.importFromXML() does not support optional elements
Summary: [PATCH] NullPointerException: XSSFImportFromXML.importFromXML() does not supp...
Status: RESOLVED FIXED
Alias: None
Product: POI
Classification: Unclassified
Component: XSSF (show other bugs)
Version: 3.9-FINAL
Hardware: PC All
: P2 major with 4 votes (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords: PatchAvailable
Depends on:
Blocks:
 
Reported: 2013-12-09 22:37 UTC by Thomas Kessler
Modified: 2014-10-21 15:56 UTC (History)
1 user (show)



Attachments
ZIP containing an XML Schema with optional element, an XML file that validates against this schema and an Excel file with XML Maps using 'mapping.xsd' (7.90 KB, application/x-zip-compressed)
2013-12-09 22:37 UTC, Thomas Kessler
Details
Patch containing a bugfix and a unit test (8.62 KB, application/octet-stream)
2014-01-20 17:05 UTC, Thomas Kessler
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Thomas Kessler 2013-12-09 22:37:34 UTC
Created attachment 31102 [details]
ZIP containing an XML Schema with optional element, an XML file that validates against this schema and an Excel file with XML Maps using 'mapping.xsd'

Please see the contents of the zip file attached to this bug...

The XML Schema file 'mapping.xsd' contains an optional element:
 <xs:element name="OptionalRating" type="xs:decimal" minOccurs="0"/>

The XML file 'data.xml' validates successfully against 'mapping.xsd'. It does not contain the element 'OptionalRating'.

Importing 'data.xml' into an Excel file with XML maps using 'mapping.xsd' with XSSFImportFromXML.importFromXML(xmlInputString) fails with a NullPointerException because the code assumes that all elements declared in the XML Schema file must also be present in the XML data file (which is not necessarily the case if elements are declared optional.

The exception occurs on the following code line in method "XSSFImportFromXML.importFromXML(xmlInputString)":
 Node result = (Node) xpath.evaluate(xpathString, doc, XPathConstants.NODE);
 // result can be null with optional nodes!
 String textContent = result.getTextContent(); // NPE on 'result' access

A unit test to reproduce this exception could look like this:
 InputStream xlsxInput = getResourceAsStream("excelWithMapping.xlsx");
 InputStream xmlInput = getResourceAsStream("data.xml");
 String xml = IOUtils.toString(xmlInput);
 XSSFWorkbook wb = openWorkbook(xlsxInput);
 XSSFMap map = wb.getMapInfo().getXSSFMapByName("PersonInfoRoot_Map");
 XSSFImportFromXML importer = new XSSFImportFromXML(map);
 importer.importFromXML(xml); // throws NPE


Additional info:
- used POI version: 3.9
- used Excel version: 2010
Comment 1 Thomas Kessler 2014-01-20 17:05:09 UTC
Created attachment 31237 [details]
Patch containing a bugfix and a unit test

The patch makes it possible to import XML into an Excel file which is mapped with a schema containing optional elements (minOccurs="0").
The patch also contains a new unit test (all other tests still work).
Comment 2 Dominik Stadler 2014-10-21 15:56:58 UTC
Thanks for the patch, this is now applied as r1633404.