1. 目标
使用go解析xmind,后期可以转换成excel,或者生成自动化执行步骤等
2. xmind样例
3. 代码
package main
import (
"archive/zip"
"encoding/json"
"io/ioutil"
"log"
)
const (
fullFilename = "XX系统测试用例.xmind"
contentFilename = "content.json"
)
type (
Topic struct {
Id string `json:"id"`
Title string `json:"title"`
Children struct {
Attached []Topic `json:"attached"`
} `json:"children"`
}
Sheet struct {
Id string `json:"id"`
Class string `json:"class"`
Title string `json:"title"`
RootTopic Topic `json:"rootTopic"`
}
)
func main() {
zipFp, zipErr := zip.OpenReader(fullFilename)
if zipErr != nil {
log.Fatalf("Failed to open zip file %s: %s\n", fullFilename, zipErr)
}
sheets := make([]Sheet, 0)
for _, file := range zipFp.File {
if file.FileInfo().Name() != contentFilename {
continue
}
contentFp, openErr := file.Open()
if openErr != nil {
log.Fatalf("Failed to open file %s: %s\n", contentFilename, openErr)
}
contentByte, readErr := ioutil.ReadAll(contentFp)
if readErr != nil {
log.Fatalf("Failed to read file %s: %s\n", contentFilename, readErr)
}
jsonErr := json.Unmarshal(contentByte, &sheets)
if jsonErr != nil {
log.Fatalf("Failed to parse json to Sheet: %s\n", jsonErr)
}
prettyJson, _ := json.MarshalIndent(sheets, "", " ")
log.Println(string(prettyJson))
}
}
输出
[
{
"id": "4bf95f2fd57af298967acae36d",
"class": "sheet",
"title": "画布 1",
"rootTopic": {
"id": "b9aa22deba98b3b20c7ac8aca2",
"title": "XX系统",
"children": {
"attached": [
{
"id": "b58888b5ceebbf0e68dada0656",
"title": "登录模块",
"children": {
"attached": [
{
"id": "2682d182-9543-49ce-86bd-b7752da1bfd7",
"title": "登录功能",
"children": {
"attached": [
{
"id": "78c6cd8f-077d-4134-ae67-b4ddb7fbadb5",
"title": "正确用户名密码, 登录成功",
"children": {
"attached": [
......