fix XmlUtil

This commit is contained in:
Looly 2019-12-11 11:46:15 +08:00
parent ede0d418f1
commit f80e3790ae

View File

@ -5,6 +5,7 @@ import cn.hutool.core.exceptions.UtilException;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.map.MapUtil;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
@ -730,6 +731,7 @@ public class XmlUtil {
* @return XML数据转换后的Map
* @since 4.0.8
*/
@SuppressWarnings("unchecked")
public static Map<String, Object> xmlToMap(Node node, Map<String, Object> result) {
if (null == result) {
result = new HashMap<>();
@ -740,32 +742,32 @@ public class XmlUtil {
Element childEle;
for (int i = 0; i < length; ++i) {
childNode = nodeList.item(i);
if (isElement(childNode)) {
if (false == isElement(childNode)) {
continue;
}
childEle = (Element) childNode;
if (childEle.hasChildNodes() && isElement(childEle.getChildNodes().item(0))) {
final Object value = result.get(childEle.getNodeName());
if (null != value) {
if (value instanceof List) {
((List) value).add(xmlToMap(childEle,new HashMap<>()));
} else {
result.put(childEle.getNodeName(), CollUtil.newArrayList(value, xmlToMap(childEle,new HashMap<>())));
Object newValue = null;
if (childEle.hasChildNodes()) {
// 子节点继续递归遍历
final Map<String, Object> map = xmlToMap(childEle);
if (MapUtil.isNotEmpty(map)) {
newValue = map;
}
} else {
result.put(childEle.getNodeName(), xmlToMap(childEle,new HashMap<>()));
}
} else {
final Object value = result.get(childEle.getNodeName());
if (null != value) {
if (value instanceof List) {
((List) value).add(childEle.getTextContent());
} else {
result.put(childEle.getNodeName(), CollUtil.newArrayList(value, childEle.getTextContent()));
}
} else {
result.put(childEle.getNodeName(), childEle.getTextContent());
}
newValue = childEle.getTextContent();
}
if (null != newValue) {
if (null != value) {
if (value instanceof List) {
((List<Object>) value).add(newValue);
} else {
result.put(childEle.getNodeName(), CollUtil.newArrayList(value, newValue));
}
} else {
result.put(childEle.getNodeName(), newValue);
}
}
}
return result;
@ -859,8 +861,8 @@ public class XmlUtil {
* @since 5.0.4
*/
public static Element appendChild(Node node, String tagName, String namespace) {
Document doc = (node instanceof Document) ? (Document) node : node.getOwnerDocument();
Element child = (null == namespace) ? doc.createElement(tagName) : doc.createElementNS(namespace, tagName);
final Document doc = (node instanceof Document) ? (Document) node : node.getOwnerDocument();
final Element child = (null == namespace) ? doc.createElement(tagName) : doc.createElementNS(namespace, tagName);
node.appendChild(child);
return child;
}
@ -875,38 +877,39 @@ public class XmlUtil {
* @param data Map类型数据
* @since 4.0.8
*/
@SuppressWarnings("rawtypes")
private static void mapToXml(Document doc, Element element, Map<?, ?> data) {
Element filedEle;
Object key;
for (Entry<?, ?> entry : data.entrySet()) {
key = entry.getKey();
if (null != key) {
// key作为标签名
if (null == key) {
continue;
}
// key作为标签名无值的节点作为空节点创建
filedEle = doc.createElement(key.toString());
element.appendChild(filedEle);
final Object value = entry.getValue();
// value作为标签内的值
if (null != value) {
if(value instanceof List){
for(int i =0;i<((List) value).size();i++){
if (((List) value).get(i) instanceof Map) {
final Object value = entry.getValue();
if (null == value) {
continue;
}
if (value instanceof List) {
for (Object listEle : (List) value) {
if (listEle instanceof Map) {
// 如果值依旧为map递归继续
mapToXml(doc, filedEle, (Map<?, ?>) ((List) value).get(i));
element.appendChild(filedEle);
mapToXml(doc, filedEle, (Map<?, ?>) listEle);
} else {
// 创建文本节点
filedEle.appendChild(doc.createTextNode(value.toString()));
}
}
}else {
if (value instanceof Map) {
} else if (value instanceof Map) {
// 如果值依旧为map递归继续
mapToXml(doc, filedEle, (Map<?, ?>) value);
element.appendChild(filedEle);
} else {
filedEle.appendChild(doc.createTextNode(value.toString()));
}
}
}
}
}
}