Fleets is similar but simpler than army's change so here I describe only the army change case. Every army/fleet have a leader character (general, admiral, colonel, captain, etc.) and units. In campaign map check for the leader character name and note it. Find the character record in CHARACTER_ARRAY checking for his name in CAMPAIGN_LOCALISATON. In the 3rd line of CHARACTER_ARRAY/CHARACTER is the ARMY ID entry. Using this get in ARMY_ARRAY and spot the army. In ARMY_ARRAY/ARMY 4th line is 0 or a settlement ID if the army is stationed inside a settlement. Since you probably want to change the position also, enter 0 or new settlement ID. Get further in UNITs records and in COMMANDER_DETAILS change the owner faction name. Repeat for every UNIT of the army.
Now get in [..] CAMPAIGN_ENV/CAMPAIGN_MODEL/CAI_INTERFACE/CAI_WORLD/CAI_WORLD_SETTLEMENTS and find the settlement cai record that the army is stationed in. In CAI_GARRISONABLE 1st entry is the CAI_ARMY ID. Write it down and enter 0. If the army is not inside a settlement you still need to do this in any settlement of the same faction that has an army stationed in. Write down the CAI_ARMY ID and leave it as it is.
Get in [..] CAMPAIGN_ENV/CAMPAIGN_MODEL/CAI_INTERFACE/CAI_WORLD/CAI_RESAURCE_MOBILES (these are the cai army/fleet records), and spot the record of the army using the CAI_ARMY ID you have already. If the army was not inside a settlement use the CAI_ARMY ID of the other army you found in previous step, search the nearby records checking CAI_RESAURCE_MOBILES/CAI_RESAURCE_MOBILE 10th entry which is the army ID (found in [..] CAMPAIGN_ENV/CAMPAIGN_MODEL/WORLD/FACTION_ARRAY/FACTION_ARRAY/FACTION/ARMY_ARRAY/ARMY_ARRAY/ARMY 3rd line).
When you find the right CAI_RESAURCE_MOBILES get in OWNED_DIRECT and enter CAI_FACTION ID of new faction. In CAI_SITUATED change both coordinates (1st and 2nd entries) and CAI_REGION ID for the new place of the army.
In CAI_RESAURCE_MOBILES/CAI_RESAURCE_MOBILE 2nd entry enter 0 or the new settlement CAI ID if you want to place it in a settlement. In that case you have to return in CAI_SETTLEMENT record and place the CAI_ARMY ID in CAI_GARRISONABLE 1st entry.