public NodeList queryAbstractElementDocElem( String sheetName, String queryExpression, String requiredField) throws Exception { if (!queryExpression.contains(Constants.QUERY_EQUALS_EXP)) { throw new IllegalArgumentException("expression should contain =", null); } ArrayList<String> keyValSequence = new ArrayList<String>(); if (queryExpression.contains(Constants.QUERY_CONCAT_EXP)) { String[] expressionArray_joined = queryExpression.split(Constants.QUERY_CONCAT_EXP); for (String equalsExpression : expressionArray_joined) { String[] expressionArray_equals = equalsExpression.split(Constants.QUERY_EQUALS_EXP); keyValSequence.add(expressionArray_equals[0]); keyValSequence.add(expressionArray_equals[1]); } } else { String[] expressionArray_equals = queryExpression.split(Constants.QUERY_EQUALS_EXP); keyValSequence.add(expressionArray_equals[0]); keyValSequence.add(expressionArray_equals[1]); } String[] expressionArray1 = {}; String[] e = keyValSequence.toArray(expressionArray1); this.sheetName = sheetName; String toRet = ""; if (docNew == null) { docNew = getSimpleXmlFromXls(); RobotCache.getXmlCache().put(cacheId, docNew); docNew = RobotCache.getXmlCache().get(cacheId); } else { log.debug("Working with docNew from cache"); } XPath xpath = XPathFactory.newInstance().newXPath(); // String query = "//Row/Record[(@key=\"" +expressionArray[0]+"\" and @value=\"" // +expressionArray[1]+"\" )" + // " and (@key=\"" +expressionArray[2]+"\" and @value=\"" +expressionArray[3]+"\")"+ // "]/../Record[@key=\"" +requiredField+"\"]"; String query = ""; if (e.length > 2) { query = "//Row[" + e[0] + "='" + e[1] + "' and " + e[2] + "='" + e[3] + "']"; } else { query = "//Row[" + e[0] + "='" + e[1] + "']"; } log.debug("Query is " + query); XPathExpression expr = xpath.compile(query); Object result = expr.evaluate(docNew, XPathConstants.NODESET); NodeList tableRowNodes = (NodeList) result; log.info("returning " + tableRowNodes); return tableRowNodes; }
public Document queryAbstractElementDocDocument( String sheetName, String queryExpression, String requiredField) throws Exception { NodeList tableRowNodes = queryAbstractElementDocElem(sheetName, queryExpression, requiredField); DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); Document doc = docBuilder.newDocument(); Element rootElement = docNew.createElement("Result"); log.info("returning " + tableRowNodes); for (int resultSetIdx = 0; resultSetIdx < tableRowNodes.getLength(); resultSetIdx++) { Element record = (Element) tableRowNodes.item(resultSetIdx); rootElement.appendChild(record); } // doc.appendChild(rootElement); return docNew; }