ソースを参照

fix xml null bug

Looly 5 年 前
コミット
96c56d9e31

+ 1 - 0
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)

+ 19 - 13
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

+ 9 - 0
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;
@@ -65,6 +66,14 @@ public class XmlUtilTest {
 	}
 
 	@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 = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"//
 				+ "<returnsms>"//

+ 4 - 0
hutool-core/src/test/resources/test.xml

@@ -1,4 +1,8 @@
 <?xml version="1.0" encoding="utf-8" standalone="no"?>
+<!-- returnstatus 状态
+	 message 消息
+-->
+
 <returnsms>
 <returnstatus>Success(成功)</returnstatus>
 <message>ok</message>