mirror of
https://gitee.com/dromara/hutool.git
synced 2025-05-02 20:02:49 +08:00
fix xml null bug
This commit is contained in:
parent
c19176c8e8
commit
96c56d9e31
@ -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)
|
||||
|
@ -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
|
||||
|
@ -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 = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"//
|
||||
|
@ -1,4 +1,8 @@
|
||||
<?xml version="1.0" encoding="utf-8" standalone="no"?>
|
||||
<!-- returnstatus 状态
|
||||
message 消息
|
||||
-->
|
||||
|
||||
<returnsms>
|
||||
<returnstatus>Success(成功)</returnstatus>
|
||||
<message>ok</message>
|
||||
|
Loading…
Reference in New Issue
Block a user