mirror of https://github.com/halo-dev/halo
Fixed XmlTransferMapUtils.
parent
4b0b9d5245
commit
c52e295f9a
|
@ -1,22 +1,28 @@
|
|||
package xyz.guqing.transfer.processor;
|
||||
package run.halo.app.utils;
|
||||
|
||||
import org.dom4j.Document;
|
||||
import org.dom4j.Element;
|
||||
import org.dom4j.io.SAXReader;
|
||||
|
||||
import java.io.*;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 解析wordpress导出的xml文章数据为Map结果集
|
||||
* @uthor guqing
|
||||
* 解析 WordPress 导出的 xml 文章数据为 Map 结果集
|
||||
*
|
||||
* @author guqing
|
||||
*/
|
||||
public class TransferMapProcessor {
|
||||
// 存储在此集合中的节点名称都会被解析为一个List存储
|
||||
private static final List<String> ARRAY_PROPERTY = Arrays.asList(new String[]{"channel", "item", "category", "postmeta", "comment"});
|
||||
public class XmlTransferMapUtils {
|
||||
|
||||
/**
|
||||
* 存储在此集合中的节点名称都会被解析为一个List存储
|
||||
*/
|
||||
private static final List<String> ARRAY_PROPERTY = Arrays.asList("channel", "item", "category", "postmeta", "comment");
|
||||
|
||||
/**
|
||||
* 根据xml文件对象获取 xml 的根节点 rootElement 对象
|
||||
*
|
||||
* @param file xml的文件对象
|
||||
* @return 返回根节点元素
|
||||
*/
|
||||
|
@ -25,9 +31,7 @@ public class TransferMapProcessor {
|
|||
SAXReader saxReader = new SAXReader();
|
||||
FileInputStream fileInputStream = new FileInputStream(file);
|
||||
Document document = saxReader.read(fileInputStream);
|
||||
Element rootElement = document.getRootElement();
|
||||
|
||||
return rootElement;
|
||||
return document.getRootElement();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("can not get root element");
|
||||
}
|
||||
|
@ -36,12 +40,12 @@ public class TransferMapProcessor {
|
|||
|
||||
/**
|
||||
* 根据根节点获取子节点元素集合递归遍历得到Map结果集
|
||||
*
|
||||
* @param root xml的根节点元素对象
|
||||
* @return 返回解析xml得到的Map结果集
|
||||
*/
|
||||
public Map<String, Object> getResultSetMapping(Element root) {
|
||||
Map<String, Object> result = new HashMap<String, Object>();
|
||||
|
||||
Map<String, Object> result;
|
||||
try {
|
||||
// 获取根元素的所有子元素
|
||||
List<Element> children = root.elements();
|
||||
|
@ -57,6 +61,7 @@ public class TransferMapProcessor {
|
|||
|
||||
/**
|
||||
* 递归解析xml,实现N层解析
|
||||
*
|
||||
* @param elements 所有子节点元素集,随着递归遍历而改变
|
||||
* @param list 存储中间遍历结果的容器
|
||||
* @return 返回递归完成后的Map结果集映射
|
||||
|
@ -78,7 +83,7 @@ public class TransferMapProcessor {
|
|||
//如果存在,合并
|
||||
if (object != null) {
|
||||
List<Map<String, Object>> olist = (List<Map<String, Object>>) object;
|
||||
olist.add(subMap);//
|
||||
olist.add(subMap);
|
||||
map.put(name, olist);
|
||||
} else {
|
||||
//否则直接存入map
|
||||
|
@ -94,7 +99,6 @@ public class TransferMapProcessor {
|
|||
if (list != null) {
|
||||
list.add(map);
|
||||
}
|
||||
|
||||
//返回结果集合
|
||||
return map;
|
||||
}
|
Loading…
Reference in New Issue