Obtaining the Account balances

I need to know the balances of several accounts in the same .KMY file in an external application. Upon uncompressing and opening, I was expecting a ‘total’ record for each account. But alas, it seems I will have to parse through the XML file, and where the set has the same Account ID#, total the amount/s. There is Python code available to parse XML files.

Are there other methods to do this ? Run a report externally ?

As you discovered, KMM does not store account balances, it calculates them when it reads in a data file. If you want to get balances from the XML file directly, you will have to do what KMM itself does, which is to add up the amounts for every split for that account, possibly taking into account currency conversions. Unfortunately, it is also not currently possible to run reports externally or from command line, although I believe there is a wishlist filed for the latter, or some variant.

1 Like

Thanks @ostroffjh . Yes, total up the balances by parsing through the splits will be quick and easy. There are no currency conversions fortunately. Each time I use KMM I back it up, so will look at uncompress and read the backup. best to stay clear of the ‘real’ file.

I may have a quick look to see if KMM has progressed with the budget/forecasting side of things, as it would be far easier to do it all in a KMM report. But I think , when I last looked at doing it in a report in KMM, I needed to specify periods, rather than use calendar months as KMM does.

By ‘progressed’, an old post at Spreadsheet replacement for budget ? • KDE Community Forums

Have been using Python and BeatifulSoup to parse the XML file of KMyMoney.

One part of the SPLIT set are scheduled transactions; they were throwing the total balances out. Are splits for scheduled transactions the only sub set that have the ACTION in them ? An example …

You only want to take a look at splits that are inside the

<TRANSACTIONS>
:
</TRANSACTIONS>

element. You should skip the ones contained in

<SCHEDULES>
:
</SCHEDULES>

BTW, your example

<SPLIT id="S0002" account="A000434" number="3" bankid="" memo="&quot;L&quot; Value pack" reconcileflag="0" value="25/1" reconciledate="" action="" shares="25/1" payee="" price="1/1"/>

did not make it because you did not mark it as code (enclose in single back ticks ` ). Just by looking at this SPLIT you cannot tell if it is part of a transaction or schedule.

1 Like

Thanks @ipwizard . Yes, it is a bit tricky, even in the <SCHEDULES> there are <TRANSACTION> sub sets. Plus the Python code is not a sequential read, to be able to ‘bypass’ all schedules, as I see they are all together. It is using a filter, and only processing <SPLIT> tags.

To go 'up 2 levels (2 parents) on either the scheduled set or the trans set, it is the same tag … <TRANSACTION>. One level up for both is also <SPLITS> in both.

It may be easier to filter out the <SCHEDULES> to </SCHEDULES> in one pass, and then read that filtered ‘set’.

It’s been a while since I’ve parsed XML (using the a Perl module) and if I remember correctly, that had the ability to refer to or filter on tags higher in the XML. I wouldn’t be surprised if beautifulsoup had a similar search/filter capability, although I’ve never used it.

1 Like

Yes @ostroffjh , BeautifulSoup calls them parents. However the number of descendants is different for <TRANSACTION> in schedules and <TRANSACTION> in the splits that I want. So, will simply read the KMM file and rewrite it to include only the ‘splits’ I need, and then use the BeautifulSoup functions on the 2nd parse. The file is only 11Mb uncompressed, so processing only takes 1 second.

I believe you will find that the “top” level parent of the splits you want is <TRANSACTIONS> (note the plural) and the ones you do not want are under <SCHEDULES>. So, any <SPLIT> with <TRANSACTIONS> as a parent is good, but not any with <SCHEDULES>. No need to directly deal with <TRANSACTION> (in the singular.)

Of course your way is also perfectly acceptable - the main criteria is just that it does what you want. :slight_smile:

[edit to make tags in angle brackets properly visible]

1 Like

Yes @ostroffjh , that is exactly the specifications I need.

The code I use is not doing much processing, so KMM does a very good job of parsing through the entire file and getting the account balances. Only 2 seconds at the most. Maybe C# is more efficient than Python. :slight_smile:

Oops. Not sure why I didn’t notice the problem, which was clearly visible (or not) in the preview area. Edited to properly display the tags.

In comparing the schedules in KMM, to what is in the XML data, I noticed that there are old memos that aren’t being updated. Data …

<SCHEDULED_TX id="SCH000001" type="2" endDate="" occurence="4" occurenceMultiplier="2" autoEnter="1" paymentType="2" lastPayment="2024-11-19" weekendOption="2" name="Misc. payment" fixed="1" startDate="2023-12-05" lastDayInMonth="0"> <PAYMENTS/> <TRANSACTION postdate="2024-12-03" id="" memo="old memo/description ??" entrydate="" commodity="AUD"> <SPLITS>