标签:输入流 string pat 节点 val end reader cti span
目的:减少重复性代码,增加快捷高效的复制粘贴
所需要的依赖
<dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>jaxen</groupId> <artifactId>jaxen</artifactId> <version>1.1.6</version> </dependency>
具体方法代码
/** * 将输入流解析成Document * @param inputStream * @return * @throws DocumentException */ public static Document parse(InputStream inputStream) { Document document = null; try { SAXReader saxReader = new SAXReader(); document = saxReader.read(inputStream); } catch (DocumentException e) { throw new BusinessException("parse file exception:",e); } return document; } /** * 根据xpath表达式获取Document中所有的节点元素 * @param document * @param xpathExp * @return */ public static List<Element> getNodes(Document document, String xpathExp) { return document.selectNodes(xpathExp); } /** * 根据xpath表达式获取Document中所有对应节点的值 * * @param document * @param xpathExp * @return * @throws DocumentException */ public static List<String> getNodeValues(Document document, String xpathExp) { List<Element> elements = document.selectNodes(xpathExp); List<String> nodeValues = new ArrayList<>(elements.size()); for (Element element : elements) { nodeValues.add(element.getText()); } return nodeValues; } /** * 根据xpath表达式获取Document中对应节点的值 * * @param document * @param xpathExp * @return * @throws DocumentException */ public static String getNodeValue(Document document, String xpathExp) { List<Element> elements = document.selectNodes(xpathExp); if(CollectionUtils.isEmpty(elements)){ return null; } return elements.get(0).getText(); }
标签:输入流 string pat 节点 val end reader cti span
原文地址:https://www.cnblogs.com/fallmwu/p/14626520.html