diff --git a/CHANGELOG.md b/CHANGELOG.md index bbf394783..e616c389d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ * 【cron 】 修复更改系统时间后CronTimer被阻塞的问题(issue#838@Github) * 【db 】 修复Page.addOrder无效问题(issue#I1F9MZ@Gitee) * 【json 】 修复JSONConvert转换日期空指针问题(issue#I1F8M2@Gitee) +* 【core 】 修复XML中带注释Xpath解析导致空指针问题(issue#I1F2WI@Gitee) ------------------------------------------------------------------------------------------------------------- ## 5.3.1 (2020-04-17) diff --git a/hutool-core/src/main/java/cn/hutool/core/util/XmlUtil.java b/hutool-core/src/main/java/cn/hutool/core/util/XmlUtil.java index ff26635b5..c7e6bd7bf 100644 --- a/hutool-core/src/main/java/cn/hutool/core/util/XmlUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/util/XmlUtil.java @@ -1253,18 +1253,23 @@ public class XmlUtil { * @param attributesOnly, if true no recursion happens */ private void examineNode(Node node, boolean attributesOnly) { - NamedNodeMap attributes = node.getAttributes(); - for (int i = 0; i < attributes.getLength(); i++) { - Node attribute = attributes.item(i); - storeAttribute(attribute); + final NamedNodeMap attributes = node.getAttributes(); + if(null != attributes){ + for (int i = 0; i < attributes.getLength(); i++) { + Node attribute = attributes.item(i); + storeAttribute(attribute); + } } if (false == attributesOnly) { - NodeList childNodes = node.getChildNodes(); - for (int i = 0; i < childNodes.getLength(); i++) { - Node item = childNodes.item(i); - if (item.getNodeType() == Node.ELEMENT_NODE) - examineNode(item, false); + final NodeList childNodes = node.getChildNodes(); + if(null != childNodes){ + Node item; + for (int i = 0; i < childNodes.getLength(); i++) { + item = childNodes.item(i); + if (item.getNodeType() == Node.ELEMENT_NODE) + examineNode(item, false); + } } } } @@ -1276,12 +1281,13 @@ public class XmlUtil { * @param attribute to examine */ private void storeAttribute(Node attribute) { + if(null == attribute){ + return; + } // examine the attributes in namespace xmlns - if (attribute.getNamespaceURI() != null - && attribute.getNamespaceURI().equals( - XMLConstants.XMLNS_ATTRIBUTE_NS_URI)) { + if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(attribute.getNamespaceURI())) { // Default namespace xmlns="uri goes here" - if (attribute.getNodeName().equals(XMLConstants.XMLNS_ATTRIBUTE)) { + if (XMLConstants.XMLNS_ATTRIBUTE.equals(attribute.getNodeName())) { prefixUri.put(DEFAULT_NS, attribute.getNodeValue()); } else { // The defined prefixes are stored here diff --git a/hutool-core/src/test/java/cn/hutool/core/util/XmlUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/util/XmlUtilTest.java index 323619213..0838cca6b 100644 --- a/hutool-core/src/test/java/cn/hutool/core/util/XmlUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/util/XmlUtilTest.java @@ -1,6 +1,7 @@ package cn.hutool.core.util; import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.io.resource.ResourceUtil; import cn.hutool.core.map.MapBuilder; import cn.hutool.core.map.MapUtil; import org.junit.Assert; @@ -64,6 +65,14 @@ public class XmlUtilTest { Assert.assertEquals("ok", value); } + @Test + public void xpathTest2() { + String result = ResourceUtil.readUtf8Str("test.xml"); + Document docResult = XmlUtil.parseXml(result); + Object value = XmlUtil.getByXPath("//returnsms/message", docResult, XPathConstants.STRING); + Assert.assertEquals("ok", value); + } + @Test public void xmlToMapTest() { String xml = ""// diff --git a/hutool-core/src/test/resources/test.xml b/hutool-core/src/test/resources/test.xml index d831b2820..dc1eb8f6d 100644 --- a/hutool-core/src/test/resources/test.xml +++ b/hutool-core/src/test/resources/test.xml @@ -1,4 +1,8 @@ + + Success(成功) ok