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