mirror of
https://gitee.com/dromara/hutool.git
synced 2025-05-04 04:37:59 +08:00
fix xml null bug
This commit is contained in:
parent
c19176c8e8
commit
96c56d9e31
@ -16,6 +16,7 @@
|
|||||||
* 【cron 】 修复更改系统时间后CronTimer被阻塞的问题(issue#838@Github)
|
* 【cron 】 修复更改系统时间后CronTimer被阻塞的问题(issue#838@Github)
|
||||||
* 【db 】 修复Page.addOrder无效问题(issue#I1F9MZ@Gitee)
|
* 【db 】 修复Page.addOrder无效问题(issue#I1F9MZ@Gitee)
|
||||||
* 【json 】 修复JSONConvert转换日期空指针问题(issue#I1F8M2@Gitee)
|
* 【json 】 修复JSONConvert转换日期空指针问题(issue#I1F8M2@Gitee)
|
||||||
|
* 【core 】 修复XML中带注释Xpath解析导致空指针问题(issue#I1F2WI@Gitee)
|
||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
## 5.3.1 (2020-04-17)
|
## 5.3.1 (2020-04-17)
|
||||||
|
@ -1253,21 +1253,26 @@ public class XmlUtil {
|
|||||||
* @param attributesOnly, if true no recursion happens
|
* @param attributesOnly, if true no recursion happens
|
||||||
*/
|
*/
|
||||||
private void examineNode(Node node, boolean attributesOnly) {
|
private void examineNode(Node node, boolean attributesOnly) {
|
||||||
NamedNodeMap attributes = node.getAttributes();
|
final NamedNodeMap attributes = node.getAttributes();
|
||||||
|
if(null != attributes){
|
||||||
for (int i = 0; i < attributes.getLength(); i++) {
|
for (int i = 0; i < attributes.getLength(); i++) {
|
||||||
Node attribute = attributes.item(i);
|
Node attribute = attributes.item(i);
|
||||||
storeAttribute(attribute);
|
storeAttribute(attribute);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (false == attributesOnly) {
|
if (false == attributesOnly) {
|
||||||
NodeList childNodes = node.getChildNodes();
|
final NodeList childNodes = node.getChildNodes();
|
||||||
|
if(null != childNodes){
|
||||||
|
Node item;
|
||||||
for (int i = 0; i < childNodes.getLength(); i++) {
|
for (int i = 0; i < childNodes.getLength(); i++) {
|
||||||
Node item = childNodes.item(i);
|
item = childNodes.item(i);
|
||||||
if (item.getNodeType() == Node.ELEMENT_NODE)
|
if (item.getNodeType() == Node.ELEMENT_NODE)
|
||||||
examineNode(item, false);
|
examineNode(item, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method looks at an attribute and stores it, if it is a namespace
|
* This method looks at an attribute and stores it, if it is a namespace
|
||||||
@ -1276,12 +1281,13 @@ public class XmlUtil {
|
|||||||
* @param attribute to examine
|
* @param attribute to examine
|
||||||
*/
|
*/
|
||||||
private void storeAttribute(Node attribute) {
|
private void storeAttribute(Node attribute) {
|
||||||
|
if(null == attribute){
|
||||||
|
return;
|
||||||
|
}
|
||||||
// examine the attributes in namespace xmlns
|
// examine the attributes in namespace xmlns
|
||||||
if (attribute.getNamespaceURI() != null
|
if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(attribute.getNamespaceURI())) {
|
||||||
&& attribute.getNamespaceURI().equals(
|
|
||||||
XMLConstants.XMLNS_ATTRIBUTE_NS_URI)) {
|
|
||||||
// Default namespace xmlns="uri goes here"
|
// 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());
|
prefixUri.put(DEFAULT_NS, attribute.getNodeValue());
|
||||||
} else {
|
} else {
|
||||||
// The defined prefixes are stored here
|
// The defined prefixes are stored here
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package cn.hutool.core.util;
|
package cn.hutool.core.util;
|
||||||
|
|
||||||
import cn.hutool.core.collection.CollUtil;
|
import cn.hutool.core.collection.CollUtil;
|
||||||
|
import cn.hutool.core.io.resource.ResourceUtil;
|
||||||
import cn.hutool.core.map.MapBuilder;
|
import cn.hutool.core.map.MapBuilder;
|
||||||
import cn.hutool.core.map.MapUtil;
|
import cn.hutool.core.map.MapUtil;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
@ -64,6 +65,14 @@ public class XmlUtilTest {
|
|||||||
Assert.assertEquals("ok", value);
|
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
|
@Test
|
||||||
public void xmlToMapTest() {
|
public void xmlToMapTest() {
|
||||||
String xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"//
|
String xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"//
|
||||||
|
@ -1,4 +1,8 @@
|
|||||||
<?xml version="1.0" encoding="utf-8" standalone="no"?>
|
<?xml version="1.0" encoding="utf-8" standalone="no"?>
|
||||||
|
<!-- returnstatus 状态
|
||||||
|
message 消息
|
||||||
|
-->
|
||||||
|
|
||||||
<returnsms>
|
<returnsms>
|
||||||
<returnstatus>Success(成功)</returnstatus>
|
<returnstatus>Success(成功)</returnstatus>
|
||||||
<message>ok</message>
|
<message>ok</message>
|
||||||
|
Loading…
Reference in New Issue
Block a user