mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix XmlUtil
This commit is contained in:
parent
05aff7f94b
commit
83d3170942
@ -1600,8 +1600,8 @@ public class StrUtil {
|
|||||||
return EMPTY;
|
return EMPTY;
|
||||||
}
|
}
|
||||||
|
|
||||||
StringBuilder sb = new StringBuilder();
|
final StringBuilder sb = new StringBuilder();
|
||||||
int subLen = toIndex - fromIndex;
|
final int subLen = toIndex - fromIndex;
|
||||||
str.toString().codePoints().skip(fromIndex).limit(subLen).forEach(v -> sb.append(Character.toChars(v)));
|
str.toString().codePoints().skip(fromIndex).limit(subLen).forEach(v -> sb.append(Character.toChars(v)));
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ import cn.hutool.core.exceptions.UtilException;
|
|||||||
import cn.hutool.core.io.FileUtil;
|
import cn.hutool.core.io.FileUtil;
|
||||||
import cn.hutool.core.io.IoUtil;
|
import cn.hutool.core.io.IoUtil;
|
||||||
import cn.hutool.core.lang.Assert;
|
import cn.hutool.core.lang.Assert;
|
||||||
|
import cn.hutool.core.lang.Console;
|
||||||
import cn.hutool.core.map.MapUtil;
|
import cn.hutool.core.map.MapUtil;
|
||||||
import org.w3c.dom.Document;
|
import org.w3c.dom.Document;
|
||||||
import org.w3c.dom.Element;
|
import org.w3c.dom.Element;
|
||||||
@ -745,6 +746,7 @@ public class XmlUtil {
|
|||||||
if (false == isElement(childNode)) {
|
if (false == isElement(childNode)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
childEle = (Element) childNode;
|
childEle = (Element) childNode;
|
||||||
final Object value = result.get(childEle.getNodeName());
|
final Object value = result.get(childEle.getNodeName());
|
||||||
Object newValue = null;
|
Object newValue = null;
|
||||||
@ -753,11 +755,14 @@ public class XmlUtil {
|
|||||||
final Map<String, Object> map = xmlToMap(childEle);
|
final Map<String, Object> map = xmlToMap(childEle);
|
||||||
if (MapUtil.isNotEmpty(map)) {
|
if (MapUtil.isNotEmpty(map)) {
|
||||||
newValue = map;
|
newValue = map;
|
||||||
|
} else{
|
||||||
|
newValue = childEle.getTextContent();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
newValue = childEle.getTextContent();
|
newValue = childEle.getTextContent();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (null != newValue) {
|
if (null != newValue) {
|
||||||
if (null != value) {
|
if (null != value) {
|
||||||
if (value instanceof List) {
|
if (value instanceof List) {
|
||||||
|
@ -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.lang.Console;
|
||||||
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;
|
||||||
@ -74,15 +75,18 @@ public class XmlUtilTest {
|
|||||||
+ "<remainpoint>1490</remainpoint>"//
|
+ "<remainpoint>1490</remainpoint>"//
|
||||||
+ "<taskID>885</taskID>"//
|
+ "<taskID>885</taskID>"//
|
||||||
+ "<successCounts>1</successCounts>"//
|
+ "<successCounts>1</successCounts>"//
|
||||||
|
+ "<newNode><sub>subText</sub></newNode>"//
|
||||||
+ "</returnsms>";
|
+ "</returnsms>";
|
||||||
Map<String, Object> map = XmlUtil.xmlToMap(xml);
|
Map<String, Object> map = XmlUtil.xmlToMap(xml);
|
||||||
|
Console.log(map);
|
||||||
|
|
||||||
Assert.assertEquals(5, map.size());
|
Assert.assertEquals(6, map.size());
|
||||||
Assert.assertEquals("Success", map.get("returnstatus"));
|
Assert.assertEquals("Success", map.get("returnstatus"));
|
||||||
Assert.assertEquals("ok", map.get("message"));
|
Assert.assertEquals("ok", map.get("message"));
|
||||||
Assert.assertEquals("1490", map.get("remainpoint"));
|
Assert.assertEquals("1490", map.get("remainpoint"));
|
||||||
Assert.assertEquals("885", map.get("taskID"));
|
Assert.assertEquals("885", map.get("taskID"));
|
||||||
Assert.assertEquals("1", map.get("successCounts"));
|
Assert.assertEquals("1", map.get("successCounts"));
|
||||||
|
Assert.assertEquals("subText", ((Map<?, ?>)map.get("newNode")).get("sub"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
Loading…
x
Reference in New Issue
Block a user