public void setXmlString(String xmlString) throws IOException, SAXException, ParserConfigurationException { this.xmlString = xmlString; if(StringUtils.startsWith(xmlString, "file:") || StringUtils.startsWith(xmlString, "classpath:")) { ResourceEditor editor = new ResourceEditor(); editor.setAsText(xmlString); Resource resource = (Resource) editor.getValue(); xmlString = new String(IOUtils.toCharArray(resource.getInputStream())); } DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); LOG.createAppender() .appendProperty("DocumentBuilderFactory class", dbf.getClass().getName()) .appendProperty("XPathFactory class", xPathFactory.getClass().getName()) .appendBold("XML String:") .appendXML(XMLFormatter.prettyPrint(xmlString)) .log(); this.document = db.parse(new InputSource(new StringReader(xmlString))); }
private NodeList getNodeList(Element base, String xpathExpression) throws TransformerException, IOException, SAXException, XPathExpressionException, ParserConfigurationException { validate(); String baseString = XMLFormatter.prettyPrint(base); LOG.createAppender() .appendBold("Node XML String:") .appendXML(baseString) .log(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document document = db.parse(new InputSource(new StringReader(baseString))); NodeList nodeList = (NodeList) xPath.evaluate(xpathExpression, document, XPathConstants.NODESET); if (nodeList == null) { throw new IllegalArgumentException(String.format("Xpath Expression '%s' not found.", xpathExpression)); } LOG.createAppender() .appendBold("Get Node List:") .appendProperty("Xpath Expression", xpathExpression) .appendProperty("Number of Elements Found", nodeList.getLength()) .log(); return nodeList; }