From: David North Date: Tue, 3 Jan 2017 15:41:41 +0000 (+0000) Subject: Fix poor XPath performance when importing XSSF from XML X-Git-Tag: REL_3_16_BETA2~63 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=c92aaab65648c1f31547b7a49a4dc0013effa60f;p=poi.git Fix poor XPath performance when importing XSSF from XML https://bz.apache.org/bugzilla/show_bug.cgi?id=60498 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1777146 13f79535-47bb-0310-9956-ffa450edef68 --- 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 478ed54172..0ddf9fec86 100644 --- a/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFImportFromXML.java +++ b/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFImportFromXML.java @@ -132,21 +132,18 @@ public class XSSFImportFromXML { // TODO: implement support for denormalized XMLs (see // OpenOffice part 4: chapter 3.5.1.7) + Node singleNode = result.item(i).cloneNode(true); for (XSSFXmlColumnPr xmlColumnPr : table.getXmlColumnPrs()) { int localColumnId = (int) xmlColumnPr.getId(); int rowId = rowOffset + i; int columnId = columnOffset + localColumnId; String localXPath = xmlColumnPr.getLocalXPath(); - localXPath = localXPath.substring(localXPath.substring(1).indexOf('/') + 1); - - // Build an XPath to select the right node (assuming - // that the commonXPath != "/") - String nodeXPath = commonXPath + "[" + (i + 1) + "]" + localXPath; + localXPath = localXPath.substring(localXPath.substring(1).indexOf('/') + 2); // TODO: convert the data to the cell format - String value = (String) xpath.evaluate(nodeXPath, result.item(i), XPathConstants.STRING); - logger.log(POILogger.DEBUG, "Extracting with xpath " + nodeXPath + " : value is '" + value + "'"); + String value = (String) xpath.evaluate(localXPath, singleNode, XPathConstants.STRING); + logger.log(POILogger.DEBUG, "Extracting with xpath " + localXPath + " : value is '" + value + "'"); XSSFRow row = table.getXSSFSheet().getRow(rowId); if (row == null) { row = table.getXSSFSheet().createRow(rowId); 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 626e104a04..82858038b9 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFImportFromXML.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFImportFromXML.java @@ -89,7 +89,7 @@ public class TestXSSFImportFromXML { } } - @Test + @Test(timeout=10000) public void testMultiTable() throws IOException, XPathExpressionException, SAXException{ XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("CustomXMLMappings-complex-type.xlsx"); try { @@ -102,8 +102,13 @@ public class TestXSSFImportFromXML { ""+ ""+ ""+ - ""+ - ""+ + ""; + + for(int i = 10; i< 10010; i++){ + testXML += ""; + } + + testXML += ""+ ""+ ""+ ""+ @@ -127,6 +132,7 @@ public class TestXSSFImportFromXML { assertEquals(cellC7,sheet.getRow(6).getCell(2).getStringCellValue()); assertEquals(cellC8,sheet.getRow(7).getCell(2).getStringCellValue()); assertEquals(cellC9,sheet.getRow(8).getCell(2).getStringCellValue()); + assertEquals("c5001",sheet.getRow(5000).getCell(2).getStringCellValue()); } finally { wb.close(); } @@ -238,4 +244,7 @@ public class TestXSSFImportFromXML { wb.close(); } + + + }