解析文档

pom

<dependency><groupId>org.apache.commons</groupId><artifactId>commons-compress</artifactId><version>1.20</version></dependency><dependency><groupId>org.dom4j</groupId><artifactId>dom4j</artifactId><version>2.1.3</version></dependency><!--xpath不加这个依赖会报错--><dependency><groupId>jaxen</groupId><artifactId>jaxen</artifactId></dependency><!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>2.0.8</version></dependency><!-- https://mvnrepository.com/artifact/org.json/json --><dependency><groupId>org.json</groupId><artifactId>json</artifactId><version>20220320</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><!--xml to java object--><!-- https://mvnrepository.com/artifact/org.apache.commons/commons-digester3 --><dependency><groupId>org.apache.commons</groupId><artifactId>commons-digester3</artifactId><version>3.2</version></dependency>

工具类:

package com.lean.xmindParser.parser;import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;import com.alibaba.fastjson.JSON;import com.lean.xmindParser.entity.XmindRoot;
import org.dom4j.*;
import org.json.JSONObject;
import org.json.XML;/***解析xmind工具类*/
public class XmindParseUtils {public static final String xmindZenJson = "content.json";public static final String xmindLegacyContent = "content.xml";public static final String xmindLegacyComments = "comments.xml";public static String parseText(String xmindFile) throws Exception {String res = ZipUtils.extract(xmindFile);String content = null;//兼容新旧版本if (isXmindZen(res, xmindFile)) {content = getXmindZenContent(xmindFile,res);} else {content = getXmindLegacyContent(xmindFile,res);}File dir = new File(res);deleteDir(dir);XmindRoot XmindRoot = JSON.parseObject(content, XmindRoot.class);return(JSON.toJSONString(XmindRoot,false));}public static Object parseObject(String xmindFile) throws Exception {String content = parseText(xmindFile);XmindRoot XmindRoot = JSON.parseObject(content, XmindRoot.class);return XmindRoot;}public static boolean deleteDir(File dir) {if (dir.isDirectory()) {String[] children = dir.list();//递归删除目录中的子目录下for (int i = 0; i < children.length; i++) {boolean success = deleteDir(new File(dir, children[i]));if (!success) {return false;}}}// 目录此时为空,可以删除return dir.delete();}/*** @return*/public static String getXmindZenContent(String xmindFile,String extractFileDir) throws Exception {List<String> keys = new ArrayList<>();keys.add(xmindZenJson);Map<String, String> map = ZipUtils.getContents(keys, xmindFile,extractFileDir);String content = map.get(xmindZenJson);content = XmindZenUtils.getContent(content);return content;}/*** @return*/public static String getXmindLegacyContent(String xmindFile,String extractFileDir) throws Exception {List<String> keys = new ArrayList<>();keys.add(xmindLegacyContent);keys.add(xmindLegacyComments);Map<String, String> map = ZipUtils.getContents(keys, xmindFile,extractFileDir);String contentXml = map.get(xmindLegacyContent);String commentsXml = map.get(xmindLegacyComments);String xmlContent = getContent(contentXml, commentsXml);return xmlContent;}private static boolean isXmindZen(String res, String xmindFile){//解压File parent = new File(res);if (parent.isDirectory()) {String[] files = parent.list(new ZipUtils.FileFilter());for (int i = 0; i < Objects.requireNonNull(files).length; i++) {if (files[i].equals(xmindZenJson)) {return true;}}}return false;}public static String getContent(String xmlContent, String xmlComments) throws Exception {//删除content.xml里面不能识别的字符串xmlContent = xmlContent.replace("xmlns=\"urn:xmind:xmap:xmlns:content:2.0\"", "");xmlContent = xmlContent.replace("xmlns:fo=\"http://www.w3.org/1999/XSL/Format\"", "");//删除<topic>节点xmlContent = xmlContent.replace("<topics type=\"attached\">", "");xmlContent = xmlContent.replace("</topics>", "");//去除title中svg:width属性xmlContent = xmlContent.replaceAll("<title svg:width=\"[0-9]*\">", "<title>");Document document = DocumentHelper.parseText(xmlContent);// 读取XML文件,获得document对象Element root = document.getRootElement();List<Node> topics = root.selectNodes("//topic");if (xmlComments != null) {//删除comments.xml里面不能识别的字符串xmlComments = xmlComments.replace("xmlns=\"urn:xmind:xmap:xmlns:comments:2.0\"", "");Document commentDocument = DocumentHelper.parseText(xmlComments);List<Node> commentsList = commentDocument.selectNodes("//comment");for (Node topic : topics) {for (Node commentNode : commentsList) {Element commentElement = (Element) commentNode;Element topicElement = (Element) topic;if (topicElement.attribute("id").getValue().equals(commentElement.attribute("object-id").getValue())) {Element comment = topicElement.addElement("comments");comment.addAttribute("creationTime", commentElement.attribute("time").getValue());comment.addAttribute("author", commentElement.attribute("author").getValue());comment.addAttribute("content", commentElement.element("content").getText());}}}}Node rootTopic = root.selectSingleNode("/xmap-content/sheet/topic");rootTopic.setName("rootTopic");List<Node> topicList = rootTopic.selectNodes("//topic");for (Node node : topicList) {node.setName("attached");}Element sheet = root.elements("sheet").get(0);String res = sheet.asXML();JSONObject xmlJSONObj = XML.toJSONObject(res);JSONObject jsonObject = xmlJSONObj.getJSONObject("sheet");//设置缩进return jsonObject.toString(4);}
}
package com.lean.xmindParser.parser;import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.dom4j.DocumentException;
import java.io.IOException;public class XmindZenUtils {/*** @param jsonContent* @return* @throws IOException* @throws DocumentException*/public static String getContent(String jsonContent) {JSONObject jsonObject = JSONArray.parseArray(jsonContent).getJSONObject(0);JSONObject rootTopic = jsonObject.getJSONObject("rootTopic");transferNotes(rootTopic);JSONObject children = rootTopic.getJSONObject("children");recursionChildren(children);return jsonObject.toString();}/*** 递归转换children** @param children*/private static void recursionChildren(JSONObject children) {if (children == null) {return;}JSONArray attachedArray = children.getJSONArray("attached");if (attachedArray == null) {return;}for (Object attached : attachedArray) {JSONObject attachedObject = (JSONObject) attached;transferNotes(attachedObject);JSONObject childrenObject = attachedObject.getJSONObject("children");if (childrenObject == null) {continue;}recursionChildren(childrenObject);}}private static void transferNotes(JSONObject object) {JSONObject notes = object.getJSONObject("notes");if (notes == null) {return;}JSONObject plain = notes.getJSONObject("plain");if (plain != null) {String content = plain.getString("content");notes.remove("plain");notes.put("content", content);} else {notes.put("content", null);}}}
package com.lean.xmindParser.parser;import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;import org.apache.commons.compress.archivers.ArchiveException;
import org.apache.commons.compress.archivers.examples.Expander;/**** @Classname ZipUtil* @Description zip解压工具*/
public class ZipUtils {private static final String currentPath = System.getProperty("user.dir");public static Map<String,String> getContents(List<String> subFileNames, String fileName,String extractFileDir) throws Exception {String destFilePath =extractFileDir;Map<String,String> map = new HashMap<>();File destFile = new File(destFilePath);if (destFile.isDirectory()) {String[] res = destFile.list(new FileFilter());for (int i = 0; i < Objects.requireNonNull(res).length; i++) {if (subFileNames.contains(res[i])) {String s = destFilePath + File.separator + res[i];String content = getFileContent(s);map.put(res[i], content);}}}return map;}public static String extract(String fileName) throws IOException, ArchiveException {File file = new File(fileName);Expander expander = new Expander();String destFileName =currentPath +File.separator+ "XMind"+System.currentTimeMillis();expander.expand(file, new File(destFileName));return destFileName;}static class FileFilter implements FilenameFilter {@Overridepublic boolean accept(File dir, String name) {if (name.endsWith(".xml") || name.endsWith(".json")) {return true;}return false;}}public static String getFileContent(String fileName) throws IOException {File file;try {file = new File(fileName);} catch (Exception e) {throw new RuntimeException("找不到该文件");}FileReader fileReader = new FileReader(file);BufferedReader bufferedReder = new BufferedReader(fileReader);StringBuilder stringBuffer = new StringBuilder();while (bufferedReder.ready()) {stringBuffer.append(bufferedReder.readLine());}bufferedReder.close();fileReader.close();return stringBuffer.toString();}
}

实体类:

package com.lean.xmindParser.entity;
import lombok.Data;@Data
public class XmindRoot {private String id;private String title;private TopicText rootTopic;
}package com.lean.xmindParser.entity;
import lombok.Data;
import java.util.List;@Data
public class TopicText {private String id;private String title;private Notes notes;private List<Comments> comments;private Children children;private List<String> labels;
}package com.lean.xmindParser.entity;
import lombok.Data;
@Data
public class Notes {private String content;
}package com.lean.xmindParser.entity;
import lombok.Data;
@Data
public class Comments {private long creationTime;private String author;private String content;
}package com.lean.xmindParser.entity;
import lombok.Data;
import java.util.List;
@Data
public class Children {private List<TopicText> attached;
}

测试:

    public static void main(String[] args) throws Exception {String fileName = "E:\\spring-lean-xmindparser\\doc\\高等数学·上.xmind";String res = XmindParseUtils.parseText(fileName);System.out.println(res);}

代码地址

Xmind文档解析导入相关推荐

  1. office 文档解析

    最近在项目维护过程中涉及到了office各种文档的解析,捣鼓了好长时间,有点点收货,记下来. 涉及文档:doc.docx.xls.xlsx.pptx. 语言:VC 下面对不同的文档进行简单解析: 一. ...

  2. VC++ MSXML创建XML文件以及对XML文档解析

    VC++ MSXML创建XML文件以及对XML文档解析 转自http://www.newxing.com/Tech/Program/Cpp/703.html // XmlCreationDemo.cp ...

  3. java docx文档解析_带有docx4j的Java Word(.docx)文档

    java docx文档解析 几个月前,我需要创建一个包含许多表和段落的动态Word文档. 过去,我曾使用POI来实现此目的,但是我发现它很难使用,并且在创建更复杂的文档时对我来说效果不佳. 因此,对于 ...

  4. 带你看论文丨全局信息对于图网络文档解析的影响

    摘要:文档理解着重于从非结构化文档中识别并提取键值对信息,并将其输出为结构化数据.在过往的信息提取中,大多数工作仅仅只关注于提取文本的实体关系,因此并不适用于直接用于文档理解上. 本文分享自华为云社区 ...

  5. [unity3d] iTween文档解析(2) (iTween方法和属性)

    [unity3d] iTween文档解析(2) (iTween方法和属性): DrawLine:为OnDrawGizmos() 调用(注意此方法只能在OnDrawGizmos()和 OnDrawGiz ...

  6. fmod文档解析音频_将音频插入Word 2007文档

    fmod文档解析音频 A cool trick I learned the other day is inserting an audio file into word documents.  The ...

  7. hutool导出excel大数据_HuTool工具类使用之Excel文档的导入导出

    HuTool工具类使用之Excel文档的导入导出 前言 在日常的工作开发中,Excel的导入和导出是必不可少的,如果自己写相应的导入导出方法,会显得十分繁琐,本文采用Hutool工具类实现的Excel ...

  8. pdf文档解析相关工具包

    pdf文档解析相关工具包 pdf生成 fdfgen: 能够自动创建pdf文档,并填写信息 pdf表格解析 pdftabextract: 用于OCR识别后的表格信息解析,很强大 tabula-py: 直 ...

  9. WebService --WSDL文档解析

    通俗的讲:WSDL文档描述了Web Service如下3个方面: WHAT--该 Web Service包含"什么"操作 HOW--该 Web Service的操作应该" ...

最新文章

  1. python怎么画条形图-如何在Bokeh(Python)中绘制水平条形图
  2. wpf学习笔记---初识xaml标签语言
  3. CCNP之IS-IS实验
  4. os模块:与操作系统交互的一个接口
  5. 卡拉丁发布第四代车用空调滤清器
  6. 【转载】早点长大的飞秋
  7. 船舶定位实时查询系统_港口人员精准定位系统,实时安全管控与智能预警
  8. java解析xml实例——获取天气信息
  9. 从考勤管理需求说起,聊聊场景的思维“工具”
  10. 栈的典型应用 —— 延迟缓冲
  11. 数据类型、变量和数组
  12. iphone11支持es6吗_好久没用Carplay了:IOS11导航功能不错
  13. 集线器和交换机的区别
  14. 淘宝宝贝商家编码 管理好你的宝贝
  15. DBCO-C6-NHS ester,1384870-47-6二苯基环辛炔-碳6-琥珀酰亚胺酯仅用于科研,不用于人体和生产
  16. 3个重要的同余式定理
  17. 修改Windows的Internet时间服务器使时间同步
  18. 单片机理论篇(未完成)
  19. Android生日礼物(含拼图游戏,背景音乐,自动拨号等功能实现)--根据代码规范修改注释以及定义
  20. uniapp生成随机数字

热门文章

  1. 手机提醒便签设置周末免打扰模式在哪里
  2. 广告内容定向分级,保护未成年人身心健康
  3. java_05手机里练习
  4. 深入浅出- ABP 多租户
  5. 你为什么应该为软件付费
  6. APP技术选型,原生APP、混合APP及跨平台应用介绍
  7. matlab中的while循环语句,Matlab的if语句switch语句for循环while循环语句练习
  8. Minecraft Fabric Mod编写日记-02创建一个物品
  9. Jenkins 教程(安装和解锁)
  10. PyCharm添加Anaconda中的虚拟环境,Python解释器出现Conda executable is not found