mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
fix XmlUtil
This commit is contained in:
parent
2465c024b7
commit
e8ecadd9a3
@ -20,6 +20,7 @@
|
|||||||
* 【poi 】 修复使用BigWriter写出,ExcelWriter修改单元格值失败的问题(issue#I3VSDO@Gitee)
|
* 【poi 】 修复使用BigWriter写出,ExcelWriter修改单元格值失败的问题(issue#I3VSDO@Gitee)
|
||||||
* 【jwt 】 修复Hmac算法下生成签名是hex的问题(issue#I3W6IP@Gitee)
|
* 【jwt 】 修复Hmac算法下生成签名是hex的问题(issue#I3W6IP@Gitee)
|
||||||
* 【core 】 修复TreeUtil.build中deep失效问题(issue#1661@Github)
|
* 【core 】 修复TreeUtil.build中deep失效问题(issue#1661@Github)
|
||||||
|
* 【json 】 修复XmlUtil.xmlToBean判断问题(issue#1663@Github)
|
||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
@ -1024,7 +1024,11 @@ public class XmlUtil {
|
|||||||
public static <T> T xmlToBean(Node node, Class<T> bean) {
|
public static <T> T xmlToBean(Node node, Class<T> bean) {
|
||||||
final Map<String, Object> map = xmlToMap(node);
|
final Map<String, Object> map = xmlToMap(node);
|
||||||
if (null != map && map.size() == 1) {
|
if (null != map && map.size() == 1) {
|
||||||
return BeanUtil.toBean(map.get(bean.getSimpleName()), bean);
|
final String simpleName = bean.getSimpleName();
|
||||||
|
if(map.containsKey(simpleName)){
|
||||||
|
// 只有key和bean的名称匹配时才做单一对象转换
|
||||||
|
return BeanUtil.toBean(map.get(simpleName), bean);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return BeanUtil.toBean(map, bean);
|
return BeanUtil.toBean(map, bean);
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package cn.hutool.core.util;
|
package cn.hutool.core.util;
|
||||||
|
|
||||||
|
import cn.hutool.core.bean.BeanUtil;
|
||||||
import cn.hutool.core.collection.CollUtil;
|
import cn.hutool.core.collection.CollUtil;
|
||||||
import cn.hutool.core.io.resource.ResourceUtil;
|
import cn.hutool.core.io.resource.ResourceUtil;
|
||||||
import cn.hutool.core.lang.Console;
|
import cn.hutool.core.lang.Console;
|
||||||
@ -214,6 +215,29 @@ public class XmlUtilTest {
|
|||||||
Assert.assertEquals(testBean.getBankCode(), testBean2.getBankCode());
|
Assert.assertEquals(testBean.getBankCode(), testBean2.getBankCode());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void xmlToBeanTest2(){
|
||||||
|
//issue#1663@Github
|
||||||
|
String xmlStr = "<?xml version=\"1.0\" encoding=\"gbk\" ?><response><code>02</code></response>";
|
||||||
|
|
||||||
|
Document doc = XmlUtil.parseXml(xmlStr);
|
||||||
|
|
||||||
|
// 标准方式
|
||||||
|
Map<String, Object> map = XmlUtil.xmlToMap(doc.getFirstChild());
|
||||||
|
SmsRes res = new SmsRes();
|
||||||
|
BeanUtil.fillBeanWithMap(map, res, true);
|
||||||
|
|
||||||
|
// toBean方式
|
||||||
|
SmsRes res1 = XmlUtil.xmlToBean(doc.getFirstChild(), SmsRes.class);
|
||||||
|
|
||||||
|
Assert.assertEquals(res.toString(), res1.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
static class SmsRes {
|
||||||
|
private String code;
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void cleanCommentTest() {
|
public void cleanCommentTest() {
|
||||||
final String xmlContent = "<info><title>hutool</title><!-- 这是注释 --><lang>java</lang></info>";
|
final String xmlContent = "<info><title>hutool</title><!-- 这是注释 --><lang>java</lang></info>";
|
||||||
|
@ -13,4 +13,5 @@ public class XMLTest {
|
|||||||
final String s = JSONUtil.toXmlStr(put);
|
final String s = JSONUtil.toXmlStr(put);
|
||||||
Assert.assertEquals("<aaa>你好</aaa><键2>test</键2>", s);
|
Assert.assertEquals("<aaa>你好</aaa><键2>test</键2>", s);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user