diff --git a/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFImportFromXML.java b/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFImportFromXML.java
index 534ef0d83..1cde38b30 100644
--- a/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFImportFromXML.java
+++ b/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFImportFromXML.java
@@ -97,12 +97,15 @@ public class XSSFImportFromXML {
String xpathString = singleXmlCell.getXpath();
Node result = (Node) xpath.evaluate(xpathString, doc, XPathConstants.NODE);
- String textContent = result.getTextContent();
- logger.log(POILogger.DEBUG, "Extracting with xpath " + xpathString + " : value is '" + textContent + "'");
- XSSFCell cell = singleXmlCell.getReferencedCell();
- logger.log(POILogger.DEBUG, "Setting '" + textContent + "' to cell " + cell.getColumnIndex() + "-" + cell.getRowIndex() + " in sheet "
- + cell.getSheet().getSheetName());
- cell.setCellValue(textContent);
+ // result can be null if value is optional (xsd:minOccurs=0), see bugzilla 55864
+ if (result != null) {
+ String textContent = result.getTextContent();
+ logger.log(POILogger.DEBUG, "Extracting with xpath " + xpathString + " : value is '" + textContent + "'");
+ XSSFCell cell = singleXmlCell.getReferencedCell();
+ logger.log(POILogger.DEBUG, "Setting '" + textContent + "' to cell " + cell.getColumnIndex() + "-" + cell.getRowIndex() + " in sheet "
+ + cell.getSheet().getSheetName());
+ cell.setCellValue(textContent);
+ }
}
for (XSSFTable table : tables) {
@@ -160,6 +163,7 @@ public class XSSFImportFromXML {
_docElem = doc.getDocumentElement();
}
+ @Override
public String getNamespaceURI(String prefix) {
return getNamespaceForPrefix(prefix);
}
@@ -214,11 +218,13 @@ public class XSSFImportFromXML {
}
// Dummy implementation - not used!
+ @Override
public Iterator getPrefixes(String val) {
return null;
}
// Dummy implementation - not used!
+ @Override
public String getPrefix(String uri) {
return null;
}
diff --git a/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFImportFromXML.java b/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFImportFromXML.java
index 5f39f37ce..16f1ab471 100644
--- a/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFImportFromXML.java
+++ b/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFImportFromXML.java
@@ -37,119 +37,162 @@ public class TestXSSFImportFromXML extends TestCase {
public void testImportFromXML() throws Exception{
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("CustomXMLMappings.xlsx");
-
- String name = "name";
- String teacher = "teacher";
- String tutor = "tutor";
- String cdl = "cdl";
- String duration = "duration";
- String topic = "topic";
- String project = "project";
- String credits = "credits";
-
- String testXML = ""+
- ""+name+""+
- ""+teacher+""+
- ""+tutor+""+
- ""+cdl+""+
- ""+duration+""+
- ""+topic+""+
- ""+project+""+
- ""+credits+""+
- "\u0000";
-
- XSSFMap map = wb.getMapInfo().getXSSFMapByName("CORSO_mapping");
- assertNotNull(map);
- XSSFImportFromXML importer = new XSSFImportFromXML(map);
-
- importer.importFromXML(testXML);
-
- XSSFSheet sheet=wb.getSheetAt(0);
-
- XSSFRow row = sheet.getRow(0);
- assertTrue(row.getCell(0).getStringCellValue().equals(name));
- assertTrue(row.getCell(1).getStringCellValue().equals(teacher));
- assertTrue(row.getCell(2).getStringCellValue().equals(tutor));
- assertTrue(row.getCell(3).getStringCellValue().equals(cdl));
- assertTrue(row.getCell(4).getStringCellValue().equals(duration));
- assertTrue(row.getCell(5).getStringCellValue().equals(topic));
- assertTrue(row.getCell(6).getStringCellValue().equals(project));
- assertTrue(row.getCell(7).getStringCellValue().equals(credits));
+ try {
+ String name = "name";
+ String teacher = "teacher";
+ String tutor = "tutor";
+ String cdl = "cdl";
+ String duration = "duration";
+ String topic = "topic";
+ String project = "project";
+ String credits = "credits";
+
+ String testXML = ""+
+ ""+name+""+
+ ""+teacher+""+
+ ""+tutor+""+
+ ""+cdl+""+
+ ""+duration+""+
+ ""+topic+""+
+ ""+project+""+
+ ""+credits+""+
+ "\u0000";
+
+ XSSFMap map = wb.getMapInfo().getXSSFMapByName("CORSO_mapping");
+ assertNotNull(map);
+ XSSFImportFromXML importer = new XSSFImportFromXML(map);
+
+ importer.importFromXML(testXML);
+
+ XSSFSheet sheet=wb.getSheetAt(0);
+
+ XSSFRow row = sheet.getRow(0);
+ assertTrue(row.getCell(0).getStringCellValue().equals(name));
+ assertTrue(row.getCell(1).getStringCellValue().equals(teacher));
+ assertTrue(row.getCell(2).getStringCellValue().equals(tutor));
+ assertTrue(row.getCell(3).getStringCellValue().equals(cdl));
+ assertTrue(row.getCell(4).getStringCellValue().equals(duration));
+ assertTrue(row.getCell(5).getStringCellValue().equals(topic));
+ assertTrue(row.getCell(6).getStringCellValue().equals(project));
+ assertTrue(row.getCell(7).getStringCellValue().equals(credits));
+ } finally {
+ wb.close();
+ }
}
public void testMultiTable() throws Exception{
-
-
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("CustomXMLMappings-complex-type.xlsx");
-
- String cellC6 = "c6";
- String cellC7 = "c7";
- String cellC8 = "c8";
- String cellC9 = "c9";
-
- String testXML = "" +
- ""+
- ""+
- ""+
- ""+
- ""+
- ""+
- ""+
- ""+
- ""+
- ""+
- ""+
- ""+
- ""+
- "\u0000";
-
- XSSFMap map = wb.getMapInfo().getXSSFMapByName("MapInfo_mapping");
- assertNotNull(map);
- XSSFImportFromXML importer = new XSSFImportFromXML(map);
-
- importer.importFromXML(testXML);
-
- //Check for Schema element
- XSSFSheet sheet=wb.getSheetAt(1);
-
- assertEquals(cellC6,sheet.getRow(5).getCell(2).getStringCellValue());
- assertEquals(cellC7,sheet.getRow(6).getCell(2).getStringCellValue());
- assertEquals(cellC8,sheet.getRow(7).getCell(2).getStringCellValue());
- assertEquals(cellC9,sheet.getRow(8).getCell(2).getStringCellValue());
-
-
+ try {
+ String cellC6 = "c6";
+ String cellC7 = "c7";
+ String cellC8 = "c8";
+ String cellC9 = "c9";
+
+ String testXML = "" +
+ ""+
+ ""+
+ ""+
+ ""+
+ ""+
+ ""+
+ ""+
+ ""+
+ ""+
+ ""+
+ ""+
+ ""+
+ ""+
+ "\u0000";
+
+ XSSFMap map = wb.getMapInfo().getXSSFMapByName("MapInfo_mapping");
+ assertNotNull(map);
+ XSSFImportFromXML importer = new XSSFImportFromXML(map);
+
+ importer.importFromXML(testXML);
+
+ //Check for Schema element
+ XSSFSheet sheet=wb.getSheetAt(1);
+
+ assertEquals(cellC6,sheet.getRow(5).getCell(2).getStringCellValue());
+ assertEquals(cellC7,sheet.getRow(6).getCell(2).getStringCellValue());
+ assertEquals(cellC8,sheet.getRow(7).getCell(2).getStringCellValue());
+ assertEquals(cellC9,sheet.getRow(8).getCell(2).getStringCellValue());
+ } finally {
+ wb.close();
+ }
}
public void testSingleAttributeCellWithNamespace() throws Exception{
-
-
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("CustomXMLMapping-singleattributenamespace.xlsx");
-
- String id = "a";
- String displayName = "dispName";
- String ref="19";
- String count = "21";
-
- String testXML = ""+
- ""+
- ""+
- "\u0000";
- XSSFMap map = wb.getMapInfo().getXSSFMapByName("table_mapping");
- assertNotNull(map);
- XSSFImportFromXML importer = new XSSFImportFromXML(map);
- importer.importFromXML(testXML);
-
- //Check for Schema element
- XSSFSheet sheet=wb.getSheetAt(0);
-
- assertEquals(id,sheet.getRow(28).getCell(1).getStringCellValue());
- assertEquals(displayName,sheet.getRow(11).getCell(5).getStringCellValue());
- assertEquals(ref,sheet.getRow(14).getCell(7).getStringCellValue());
- assertEquals(count,sheet.getRow(18).getCell(3).getStringCellValue());
-
+ try {
+ String id = "a";
+ String displayName = "dispName";
+ String ref="19";
+ String count = "21";
+
+ String testXML = ""+
+ ""+
+ ""+
+ "\u0000";
+ XSSFMap map = wb.getMapInfo().getXSSFMapByName("table_mapping");
+ assertNotNull(map);
+ XSSFImportFromXML importer = new XSSFImportFromXML(map);
+ importer.importFromXML(testXML);
+
+ //Check for Schema element
+ XSSFSheet sheet=wb.getSheetAt(0);
+
+ assertEquals(id,sheet.getRow(28).getCell(1).getStringCellValue());
+ assertEquals(displayName,sheet.getRow(11).getCell(5).getStringCellValue());
+ assertEquals(ref,sheet.getRow(14).getCell(7).getStringCellValue());
+ assertEquals(count,sheet.getRow(18).getCell(3).getStringCellValue());
+ } finally {
+ wb.close();
+ }
+ }
+
+
+ public void testOptionalFields_Bugzilla_55864() throws Exception {
+ XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55864.xlsx");
+ try {
+ String testXML = "" +
+ "" +
+ "" +
+ "Albert" +
+ "Einstein" +
+ "1879-03-14" +
+ "" +
+ "";
+
+ XSSFMap map = wb.getMapInfo().getXSSFMapByName("PersonInfoRoot_Map");
+ assertNotNull(map);
+ XSSFImportFromXML importer = new XSSFImportFromXML(map);
+
+ importer.importFromXML(testXML);
+
+ XSSFSheet sheet=wb.getSheetAt(0);
+
+ XSSFRow rowHeadings = sheet.getRow(0);
+ XSSFRow rowData = sheet.getRow(1);
+
+ assertEquals("FirstName", rowHeadings.getCell(0).getStringCellValue());
+ assertEquals("Albert", rowData.getCell(0).getStringCellValue());
+
+ assertEquals("LastName", rowHeadings.getCell(1).getStringCellValue());
+ assertEquals("Einstein", rowData.getCell(1).getStringCellValue());
+
+ assertEquals("BirthDate", rowHeadings.getCell(2).getStringCellValue());
+ assertEquals("1879-03-14", rowData.getCell(2).getStringCellValue());
+
+ // Value for OptionalRating is declared optional (minOccurs=0) in 55864.xlsx
+ assertEquals("OptionalRating", rowHeadings.getCell(3).getStringCellValue());
+ assertNull("", rowData.getCell(3));
+ } finally {
+ wb.close();
+ }
}
}
diff --git a/test-data/spreadsheet/55864.xlsx b/test-data/spreadsheet/55864.xlsx
new file mode 100644
index 000000000..51c9d53ca
Binary files /dev/null and b/test-data/spreadsheet/55864.xlsx differ