Xmind文档解析导入
解析文档
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文档解析导入相关推荐
- office 文档解析
最近在项目维护过程中涉及到了office各种文档的解析,捣鼓了好长时间,有点点收货,记下来. 涉及文档:doc.docx.xls.xlsx.pptx. 语言:VC 下面对不同的文档进行简单解析: 一. ...
- VC++ MSXML创建XML文件以及对XML文档解析
VC++ MSXML创建XML文件以及对XML文档解析 转自http://www.newxing.com/Tech/Program/Cpp/703.html // XmlCreationDemo.cp ...
- java docx文档解析_带有docx4j的Java Word(.docx)文档
java docx文档解析 几个月前,我需要创建一个包含许多表和段落的动态Word文档. 过去,我曾使用POI来实现此目的,但是我发现它很难使用,并且在创建更复杂的文档时对我来说效果不佳. 因此,对于 ...
- 带你看论文丨全局信息对于图网络文档解析的影响
摘要:文档理解着重于从非结构化文档中识别并提取键值对信息,并将其输出为结构化数据.在过往的信息提取中,大多数工作仅仅只关注于提取文本的实体关系,因此并不适用于直接用于文档理解上. 本文分享自华为云社区 ...
- [unity3d] iTween文档解析(2) (iTween方法和属性)
[unity3d] iTween文档解析(2) (iTween方法和属性): DrawLine:为OnDrawGizmos() 调用(注意此方法只能在OnDrawGizmos()和 OnDrawGiz ...
- fmod文档解析音频_将音频插入Word 2007文档
fmod文档解析音频 A cool trick I learned the other day is inserting an audio file into word documents. The ...
- hutool导出excel大数据_HuTool工具类使用之Excel文档的导入导出
HuTool工具类使用之Excel文档的导入导出 前言 在日常的工作开发中,Excel的导入和导出是必不可少的,如果自己写相应的导入导出方法,会显得十分繁琐,本文采用Hutool工具类实现的Excel ...
- pdf文档解析相关工具包
pdf文档解析相关工具包 pdf生成 fdfgen: 能够自动创建pdf文档,并填写信息 pdf表格解析 pdftabextract: 用于OCR识别后的表格信息解析,很强大 tabula-py: 直 ...
- WebService --WSDL文档解析
通俗的讲:WSDL文档描述了Web Service如下3个方面: WHAT--该 Web Service包含"什么"操作 HOW--该 Web Service的操作应该" ...
最新文章
- 大专学完出来学计算机,浙江2021年计算机学校读出来是什么文凭
- UA MATH563 概率论的数学基础 鞅论初步5 鞅的定义
- Tomcat源码分析(一)------ 架构
- springboot整合spring Cache(redis)
- raspbian wifi设置
- Redhat ssh服务登录慢
- 修复病毒破坏的文件关联并恢复程序图标
- 今日恐慌与贪婪指数为75 贪婪程度有所上升
- 上得写代码,下得作情诗,谁说程序员都是屌丝?
- 开课吧:分享C++代码的整洁之道!
- 我来到这世上,却不曾歌唱
- 【SSTFlashFlex51单片机烧录软件的使用方法】
- yalmip决策变量
- linux gt240驱动下载,NVIDIA官方发布Linux 256.53正式版驱动
- find 命令多条件匹配
- 自动切换输入法 for Mac(输入法辅助工具)
- 怎么在css中定义字体颜色,css中怎么设置字体颜色
- pinia报错, getActivePinia was called with no active Pinia. Did you forget to install pinia?
- 从美国创业者的成功看行业趋势
- 大文件传输软件的优势你了解吗?