mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
!82 修复XmlUtil xml转Map同名节点超过三个转list的问题
Merge pull request !82 from SunMeng/v5-dev
This commit is contained in:
commit
ede0d418f1
@ -1,49 +1,35 @@
|
||||
package cn.hutool.core.util;
|
||||
|
||||
import java.beans.XMLDecoder;
|
||||
import java.beans.XMLEncoder;
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.Reader;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import javax.xml.namespace.QName;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import javax.xml.transform.OutputKeys;
|
||||
import javax.xml.transform.Result;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
import javax.xml.xpath.XPath;
|
||||
import javax.xml.xpath.XPathConstants;
|
||||
import javax.xml.xpath.XPathExpressionException;
|
||||
import javax.xml.xpath.XPathFactory;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
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 org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
import org.xml.sax.InputSource;
|
||||
|
||||
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 javax.xml.namespace.QName;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import javax.xml.transform.*;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
import javax.xml.xpath.XPath;
|
||||
import javax.xml.xpath.XPathConstants;
|
||||
import javax.xml.xpath.XPathExpressionException;
|
||||
import javax.xml.xpath.XPathFactory;
|
||||
import java.beans.XMLDecoder;
|
||||
import java.beans.XMLEncoder;
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
/**
|
||||
* XML工具类<br>
|
||||
@ -690,8 +676,8 @@ public class XmlUtil {
|
||||
*
|
||||
* @param string 被替换的字符串
|
||||
* @return 替换后的字符串
|
||||
* @since 5.0.6
|
||||
* @see EscapeUtil#unescape(String)
|
||||
* @since 5.0.6
|
||||
*/
|
||||
public static String unescape(String string) {
|
||||
return EscapeUtil.unescape(string);
|
||||
@ -748,7 +734,6 @@ public class XmlUtil {
|
||||
if (null == result) {
|
||||
result = new HashMap<>();
|
||||
}
|
||||
|
||||
final NodeList nodeList = node.getChildNodes();
|
||||
final int length = nodeList.getLength();
|
||||
Node childNode;
|
||||
@ -757,13 +742,31 @@ public class XmlUtil {
|
||||
childNode = nodeList.item(i);
|
||||
if (isElement(childNode)) {
|
||||
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<>())));
|
||||
}
|
||||
} 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());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@ -884,6 +887,17 @@ public class XmlUtil {
|
||||
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) {
|
||||
// 如果值依旧为map,递归继续
|
||||
mapToXml(doc, filedEle, (Map<?, ?>) ((List) value).get(i));
|
||||
element.appendChild(filedEle);
|
||||
} else {
|
||||
filedEle.appendChild(doc.createTextNode(value.toString()));
|
||||
}
|
||||
}
|
||||
}else {
|
||||
if (value instanceof Map) {
|
||||
// 如果值依旧为map,递归继续
|
||||
mapToXml(doc, filedEle, (Map<?, ?>) value);
|
||||
@ -895,6 +909,7 @@ public class XmlUtil {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭XXE,避免漏洞攻击<br>
|
||||
|
Loading…
x
Reference in New Issue
Block a user