How to generate a Gramps xml file outside of Gramps?

moved from:

Rough export to Gramps XML file format

Hello.
Playing with xml content, I wonder if a “lite” skeleton (structure) of a gramps file could be useful?

Let me try to explain with an experimental mix-up of pieces of code. It has been extracted from two addons (working into gramps). So, I only tried to clean them, and tried to keep the logic. Does not work as stand-alone script.

from xml.etree import ElementTree # or from lxml import etree
from shutil import copy
entry = os.path.join($HOME, 'example.gramps')
filename = os.path.join($HOME, 'example.xml')
copy(entry, filename)
tree = ElementTree.parse(filename)
root = tree.getroot()
root.clear()
surnames = []
places = []
sources = []
the_id = 0

people = etree.SubElement(root, "people")
for s in surnames:
    the_id += 1
    person = etree.SubElement(people, "person")
    person.set('id', str(the_id) + '_' + str(len(surnames)))
    name = etree.SubElement(person, "name")
    surname = etree.SubElement(name, "surname")
    surname.text = s
pl = etree.SubElement(root, "places")
for p in places:
    the_id += 1
    place = etree.SubElement(pl, "placeobj")
    place.set('id', str(the_id) + '_' + str(len(places)))
    name = etree.SubElement(place, "pname")
    pname = name.set('value', p)
src = etree.SubElement(root, "sources")
for s in sources:
    the_id += 1
    source = etree.SubElement(src, "source")
    source.set('id', str(the_id) + '_' + str(len(sources)))
    stitle = etree.SubElement(source, "stitle")
    stitle.text = s
print(etree.tostring(root, method='xml', pretty_print=True)
root.clear()

Less than 40 lines, that’s a rough exporter for Gramps file format. :wink:

Sure, need to polish it for running as a stand-alone script for production. As you can see, that’s a poor and lazy way for quickly generating a gramps file.

Anyway, in relation with a previous question (2021) : How to generate a Gramps xml file outside of Gramps?.

On an other post, I noted a custom behavior and extra code for legacy (old XML style). During this investigation, I had to quickly export many id values for a complete importation into Gramps (mapping stuff & co). I do not have problem with that, as it was for testing (more than 76 000 ‘alone’ records ; my bad, it should be 615 surnames… there is a loop on the above lines: should look at person with the same surname before iteration).

e.g.,

unique_surnames = list(set(surnames))
unique_surnames.sort()

Finally, as Gramps has a consistent namespace, all these xml tags are always related to gramps and we could make addition of gramps xml files content, by mapping ids (not handle, more and less like gedcom ids), before an import into a Family Tree, whatever source generating this Gramps XML file.

To write a complete Gramps XML exporter does not always make sense (json, text, csv) for some small projects (open-sources or not).

Maybe having a lite skeleton also mean to have a generator identification? For knowing where this Gramps XML come from!

More advanced, an AI transformer-generator to a Gramps XML file format?

Just some thoughts.
Jérôme