elliotchance / gedcom

👪 A Go library and CLI tools for encoding, decoding, traversing, merging, comparing, querying and publishing GEDCOM files.
MIT License
98 stars 22 forks source link

Retrieve Notes from an Individual #338

Closed jorgechato closed 5 months ago

jorgechato commented 5 months ago

Do we have a straight forward way to extract the Notes for each individual? Currently I'm using the following approach which is incomplete:

func getNotes(gedcomFile *gedcom.Document, individual *gedcom.IndividualNode) {
    noteNode := gedcom.First(gedcom.NodesWithTag(individual, gedcom.TagNote))
    if noteNode != nil {
        for _, node := range gedcomFile.Nodes() {
            data, _ := node.RawSimpleNode().MarshalJSON()
            fmt.Println(string(data))
                         // this condition incomplete since noteNode can't be checked from the node pointer (which is nil)
            if node.Tag().Is(gedcom.TagNote) {
                var notes_list []string
                for _, note := range node.Nodes() {
                    notes_list = append(notes_list, note.String())
                }
                fmt.Println("Notes:")
                fmt.Println(strings.Join(notes_list, "\n"))
            }
        }
    }
}
jorgechato commented 5 months ago

Looks like I was using the wrong approach. This is a working snippet:

func getNotes(gedcomFile *gedcom.Document, individual *gedcom.IndividualNode) {
    for _, node := range individual.Nodes() {
        var notes_list []string
        if node.Tag().Is(gedcom.TagNote) {
            notes_list := append(notes_list, node.String())
            for _, note := range node.Nodes() {
                notes_list = append(notes_list, note.String())
            }
            fmt.Println("---Notes---")
            fmt.Println(strings.Join(notes_list, "\n"))
        }
    }
}

For gedcom format exported from MacFamilyTree 10 the notes are references in the individual so this approach can't be use