Created
June 10, 2021 11:43
-
-
Save dorneanu/906facb9aa2eb88c51dd348cdeaddf97 to your computer and use it in GitHub Desktop.
Convert miMind XML exports to JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module mimind2json | |
go 1.16 | |
require github.com/beevik/etree v1.1.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
github.com/beevik/etree v1.1.0 h1:T0xke/WvNtMoCqgzPhkX2r4rjY3GDZFi+FjpRZY2Jbs= | |
github.com/beevik/etree v1.1.0/go.mod h1:r8Aw8JqVegEf0w2fDnATrX9VpkMcyFeM0FhwO62wh+A= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"flag" | |
"fmt" | |
"log" | |
"strings" | |
"github.com/beevik/etree" | |
) | |
// Defines maximum level of indentation where current node should be treated | |
// as a heading | |
var maxHeadingsLevel int | |
func parseTree(e *etree.Element, treeLevel int) { | |
for _, n := range e.SelectElements("Node") { | |
title := n.SelectAttrValue("Title", "unknown") | |
char := "*" | |
var indent string | |
// For first 3 indentation levels use headings | |
// For the rest rest number of indentation | |
if treeLevel <= maxHeadingsLevel { | |
char = "!" | |
indent = strings.Repeat(char, treeLevel) | |
} else { | |
indent = strings.Repeat(char, treeLevel-maxHeadingsLevel) | |
} | |
fmt.Printf("%s %s\n", indent, title) | |
parseTree(n, treeLevel+1) | |
} | |
} | |
func main() { | |
var xmlFile string | |
flag.IntVar(&maxHeadingsLevel, "hl", 3, "Specify max heading indentation level") | |
flag.StringVar(&xmlFile, "f", "", "Specify XML file") | |
flag.Parse() | |
if len(xmlFile) == 0 { | |
log.Fatalf("No XML file specified") | |
} | |
// Read in XML file | |
doc := etree.NewDocument() | |
if err := doc.ReadFromFile(xmlFile); err != nil { | |
panic(err) | |
} | |
// Select root element | |
root := doc.SelectElement("Root") | |
content := root.SelectElement("Content") | |
parseTree(content, 1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How it works: