XML Node ersetzen

>|Sh4d0w|<

Commander
Registriert
Jan. 2009
Beiträge
2.489
Hallo zusammen,
ich möchte in meiner KeePass Export.xml bei allen Historieneinträgen Notizen löschen.
Ich habe zwar bisher schon einiges in Powershell zum Laufen gebracht, aber es sind noch Sonderfälle wie Zeilenumbrüche etc dabei und ein Bug, dass er in den falschen Key Werte ändert. Daher habe ich mal etwas mit SelectSingleNode ausprobiert, aber hier bin ich ehrlich gesagt etwas überfordert, die richtige Node (vorallem zuverlässig) anzusprechen.

[xml]$xml = (Get-Content -Path C:\test\Datenbank.xml -Encoding utf8 -raw)
$test = $xml.DocumentElement.SelectSingleNode("//History/Entry/String")
$test.ParentNode.String

Und zwar sollen Alle Values, welche unter //History/Entry/String(Key="Notes") gefunden werden, gelöscht werden.

Über die KeePass interne XML-Ersetzungsfunktion kann man nicht auf Historiennodes (geschützt) zugreifen, sonst hätte ich das dort schon geändert.

XML:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<KeePassFile>
    <Meta>
        <Generator>KeePass</Generator>
        <DatabaseName></DatabaseName>
        <DatabaseNameChanged>2021-01-30T13:55:05Z</DatabaseNameChanged>
        <DatabaseDescription></DatabaseDescription>
        <DatabaseDescriptionChanged>2021-01-30T13:55:05Z</DatabaseDescriptionChanged>
        <DefaultUserName></DefaultUserName>
        <DefaultUserNameChanged>2021-01-30T13:55:05Z</DefaultUserNameChanged>
        <MaintenanceHistoryDays>365</MaintenanceHistoryDays>
        <Color></Color>
        <MasterKeyChanged>2021-01-30T13:55:05Z</MasterKeyChanged>
        <MasterKeyChangeRec>-1</MasterKeyChangeRec>
        <MasterKeyChangeForce>-1</MasterKeyChangeForce>
        <MemoryProtection>
            <ProtectTitle>False</ProtectTitle>
            <ProtectUserName>False</ProtectUserName>
            <ProtectPassword>True</ProtectPassword>
            <ProtectURL>False</ProtectURL>
            <ProtectNotes>False</ProtectNotes>
        </MemoryProtection>
        <RecycleBinEnabled>True</RecycleBinEnabled>
        <RecycleBinUUID>LG2lFhzI3kGiWLqst+JbWg==</RecycleBinUUID>
        <RecycleBinChanged>2021-01-30T13:55:05Z</RecycleBinChanged>
        <EntryTemplatesGroup>AAAAAAAAAAAAAAAAAAAAAA==</EntryTemplatesGroup>
        <EntryTemplatesGroupChanged>2021-01-30T13:55:05Z</EntryTemplatesGroupChanged>
        <HistoryMaxItems>10</HistoryMaxItems>
        <HistoryMaxSize>6291456</HistoryMaxSize>
        <LastSelectedGroup>wdhOnziTjkCIoz/68iAlaQ==</LastSelectedGroup>
        <LastTopVisibleGroup>xiaOtrxsFUO33fgA3CS1bQ==</LastTopVisibleGroup>
        <Binaries />
        <CustomData />
    </Meta>
    <Root>
        <Group>
            <UUID>xiaOtrxsFUO33fgA3CS1bQ==</UUID>
            <Name>Datenbank</Name>
            <Notes></Notes>
            <IconID>49</IconID>
            <Times>
                <CreationTime>2021-01-30T13:55:05Z</CreationTime>
                <LastModificationTime>2021-01-30T13:55:05Z</LastModificationTime>
                <LastAccessTime>2021-01-30T13:56:10Z</LastAccessTime>
                <ExpiryTime>2021-01-30T13:54:45Z</ExpiryTime>
                <Expires>False</Expires>
                <UsageCount>18</UsageCount>
                <LocationChanged>2021-01-30T13:55:05Z</LocationChanged>
            </Times>
            <IsExpanded>True</IsExpanded>
            <DefaultAutoTypeSequence></DefaultAutoTypeSequence>
            <EnableAutoType>null</EnableAutoType>
            <EnableSearching>null</EnableSearching>
            <LastTopVisibleEntry>A9XlATS6Z0iabuHeoAu6Fw==</LastTopVisibleEntry>
            <Entry>
                <UUID>vUuElzQ3h0q92UDs8kiz+A==</UUID>
                <IconID>0</IconID>
                <ForegroundColor></ForegroundColor>
                <BackgroundColor></BackgroundColor>
                <OverrideURL></OverrideURL>
                <Tags></Tags>
                <Times>
                    <CreationTime>2021-01-30T13:55:13Z</CreationTime>
                    <LastModificationTime>2021-01-30T13:55:43Z</LastModificationTime>
                    <LastAccessTime>2021-01-30T13:55:48Z</LastAccessTime>
                    <ExpiryTime>2021-01-30T13:54:45Z</ExpiryTime>
                    <Expires>False</Expires>
                    <UsageCount>4</UsageCount>
                    <LocationChanged>2021-01-30T13:55:13Z</LocationChanged>
                </Times>
                <String>
                    <Key>Notes</Key>
                    <Value>Kommentare zum löschen</Value>
                </String>
                <String>
                    <Key>Password</Key>
                    <Value ProtectInMemory="True">Passwort</Value>
                </String>
                <String>
                    <Key>Title</Key>
                    <Value>TEST1</Value>
                </String>
                <String>
                    <Key>URL</Key>
                    <Value>https://keepass.info/</Value>
                </String>
                <String>
                    <Key>UserName</Key>
                    <Value>Benutzername</Value>
                </String>
                <AutoType>
                    <Enabled>True</Enabled>
                    <DataTransferObfuscation>0</DataTransferObfuscation>
                    <Association>
                        <Window>Zielfenster</Window>
                        <KeystrokeSequence>{USERNAME}{TAB}{PASSWORD}{TAB}{ENTER}</KeystrokeSequence>
                    </Association>
                </AutoType>
                <History>
                    <Entry>
                        <UUID>vUuElzQ3h0q92UDs8kiz+A==</UUID>
                        <IconID>0</IconID>
                        <ForegroundColor></ForegroundColor>
                        <BackgroundColor></BackgroundColor>
                        <OverrideURL></OverrideURL>
                        <Tags></Tags>
                        <Times>
                            <CreationTime>2021-01-30T13:55:13Z</CreationTime>
                            <LastModificationTime>2021-01-30T13:55:13Z</LastModificationTime>
                            <LastAccessTime>2021-01-30T13:55:13Z</LastAccessTime>
                            <ExpiryTime>2021-01-30T13:54:45Z</ExpiryTime>
                            <Expires>False</Expires>
                            <UsageCount>0</UsageCount>
                            <LocationChanged>2021-01-30T13:55:13Z</LocationChanged>
                        </Times>
                        <String>
                            <Key>Notes</Key>
                            <Value>Kommentare</Value>
                        </String>
                        <String>
                            <Key>Password</Key>
                            <Value ProtectInMemory="True">Passwort</Value>
                        </String>
                        <String>
                            <Key>Title</Key>
                            <Value>Beispieleintrag</Value>
                        </String>
                        <String>
                            <Key>URL</Key>
                            <Value>https://keepass.info/</Value>
                        </String>
                        <String>
                            <Key>UserName</Key>
                            <Value>Benutzername</Value>
                        </String>
                        <AutoType>
                            <Enabled>True</Enabled>
                            <DataTransferObfuscation>0</DataTransferObfuscation>
                            <Association>
                                <Window>Zielfenster</Window>
                                <KeystrokeSequence>{USERNAME}{TAB}{PASSWORD}{TAB}{ENTER}</KeystrokeSequence>
                            </Association>
                        </AutoType>
                    </Entry>
                    <Entry>
                        <UUID>vUuElzQ3h0q92UDs8kiz+A==</UUID>
                        <IconID>0</IconID>
                        <ForegroundColor></ForegroundColor>
                        <BackgroundColor></BackgroundColor>
                        <OverrideURL></OverrideURL>
                        <Tags></Tags>
                        <Times>
                            <CreationTime>2021-01-30T13:55:13Z</CreationTime>
                            <LastModificationTime>2021-01-30T13:55:26Z</LastModificationTime>
                            <LastAccessTime>2021-01-30T13:55:27Z</LastAccessTime>
                            <ExpiryTime>2021-01-30T13:54:45Z</ExpiryTime>
                            <Expires>False</Expires>
                            <UsageCount>2</UsageCount>
                            <LocationChanged>2021-01-30T13:55:13Z</LocationChanged>
                        </Times>
                        <String>
                            <Key>Notes</Key>
                            <Value>Kommentare</Value>
                        </String>
                        <String>
                            <Key>Password</Key>
                            <Value ProtectInMemory="True">Passwort</Value>
                        </String>
                        <String>
                            <Key>Title</Key>
                            <Value>TEST1</Value>
                        </String>
                        <String>
                            <Key>URL</Key>
                            <Value>https://keepass.info/</Value>
                        </String>
                        <String>
                            <Key>UserName</Key>
                            <Value>Benutzername</Value>
                        </String>
                        <AutoType>
                            <Enabled>True</Enabled>
                            <DataTransferObfuscation>0</DataTransferObfuscation>
                            <Association>
                                <Window>Zielfenster</Window>
                                <KeystrokeSequence>{USERNAME}{TAB}{PASSWORD}{TAB}{ENTER}</KeystrokeSequence>
                            </Association>
                        </AutoType>
                    </Entry>
                </History>
            </Entry>
            <Entry>
                <UUID>A9XlATS6Z0iabuHeoAu6Fw==</UUID>
                <IconID>0</IconID>
                <ForegroundColor></ForegroundColor>
                <BackgroundColor></BackgroundColor>
                <OverrideURL></OverrideURL>
                <Tags></Tags>
                <Times>
                    <CreationTime>2021-01-30T13:55:13Z</CreationTime>
                    <LastModificationTime>2021-01-30T13:55:32Z</LastModificationTime>
                    <LastAccessTime>2021-01-30T13:55:49Z</LastAccessTime>
                    <ExpiryTime>2021-01-30T13:54:45Z</ExpiryTime>
                    <Expires>False</Expires>
                    <UsageCount>3</UsageCount>
                    <LocationChanged>2021-01-30T13:55:13Z</LocationChanged>
                </Times>
                <String>
                    <Key>Notes</Key>
                    <Value>Alte Notizen</Value>
                </String>
                <String>
                    <Key>Password</Key>
                    <Value ProtectInMemory="True">12345</Value>
                </String>
                <String>
                    <Key>Title</Key>
                    <Value>Beispieleintrag</Value>
                </String>
                <String>
                    <Key>URL</Key>
                    <Value>https://keepass.info/help/kb/testform.html</Value>
                </String>
                <String>
                    <Key>UserName</Key>
                    <Value>Michael321</Value>
                </String>
                <AutoType>
                    <Enabled>True</Enabled>
                    <DataTransferObfuscation>0</DataTransferObfuscation>
                    <Association>
                        <Window>*Test Form - KeePass*</Window>
                        <KeystrokeSequence></KeystrokeSequence>
                    </Association>
                </AutoType>
                <History>
                    <Entry>
                        <UUID>A9XlATS6Z0iabuHeoAu6Fw==</UUID>
                        <IconID>0</IconID>
                        <ForegroundColor></ForegroundColor>
                        <BackgroundColor></BackgroundColor>
                        <OverrideURL></OverrideURL>
                        <Tags></Tags>
                        <Times>
                            <CreationTime>2021-01-30T13:55:13Z</CreationTime>
                            <LastModificationTime>2021-01-30T13:55:13Z</LastModificationTime>
                            <LastAccessTime>2021-01-30T13:55:13Z</LastAccessTime>
                            <ExpiryTime>2021-01-30T13:54:45Z</ExpiryTime>
                            <Expires>False</Expires>
                            <UsageCount>0</UsageCount>
                            <LocationChanged>2021-01-30T13:55:13Z</LocationChanged>
                        </Times>
                        <String>
                            <Key>Password</Key>
                            <Value ProtectInMemory="True">12345</Value>
                        </String>
                        <String>
                            <Key>Title</Key>
                            <Value>Beispieleintrag #2</Value>
                        </String>
                        <String>
                            <Key>URL</Key>
                            <Value>https://keepass.info/help/kb/testform.html</Value>
                        </String>
                        <String>
                            <Key>UserName</Key>
                            <Value>Michael321</Value>
                        </String>
                        <AutoType>
                            <Enabled>True</Enabled>
                            <DataTransferObfuscation>0</DataTransferObfuscation>
                            <Association>
                                <Window>*Test Form - KeePass*</Window>
                                <KeystrokeSequence></KeystrokeSequence>
                            </Association>
                        </AutoType>
                    </Entry>
                    <Entry>
                        <UUID>A9XlATS6Z0iabuHeoAu6Fw==</UUID>
                        <IconID>0</IconID>
                        <ForegroundColor></ForegroundColor>
                        <BackgroundColor></BackgroundColor>
                        <OverrideURL></OverrideURL>
                        <Tags></Tags>
                        <Times>
                            <CreationTime>2021-01-30T13:55:13Z</CreationTime>
                            <LastModificationTime>2021-01-30T13:55:21Z</LastModificationTime>
                            <LastAccessTime>2021-01-30T13:55:21Z</LastAccessTime>
                            <ExpiryTime>2021-01-30T13:54:45Z</ExpiryTime>
                            <Expires>False</Expires>
                            <UsageCount>1</UsageCount>
                            <LocationChanged>2021-01-30T13:55:13Z</LocationChanged>
                        </Times>
                        <String>
                            <Key>Notes</Key>
                            <Value>Alte Notizen</Value>
                        </String>
                        <String>
                            <Key>Password</Key>
                            <Value ProtectInMemory="True">12345</Value>
                        </String>
                        <String>
                            <Key>Title</Key>
                            <Value>Beispieleintrag #2</Value>
                        </String>
                        <String>
                            <Key>URL</Key>
                            <Value>https://keepass.info/help/kb/testform.html</Value>
                        </String>
                        <String>
                            <Key>UserName</Key>
                            <Value>Michael321</Value>
                        </String>
                        <AutoType>
                            <Enabled>True</Enabled>
                            <DataTransferObfuscation>0</DataTransferObfuscation>
                            <Association>
                                <Window>*Test Form - KeePass*</Window>
                                <KeystrokeSequence></KeystrokeSequence>
                            </Association>
                        </AutoType>
                    </Entry>
                </History>
            </Entry>
            <Group>
                <UUID>wdhOnziTjkCIoz/68iAlaQ==</UUID>
                <Name>Allgemein</Name>
                <Notes></Notes>
                <IconID>48</IconID>
                <Times>
                    <CreationTime>2021-01-30T13:55:13Z</CreationTime>
                    <LastModificationTime>2021-01-30T13:55:13Z</LastModificationTime>
                    <LastAccessTime>2021-01-30T13:56:10Z</LastAccessTime>
                    <ExpiryTime>2021-01-30T13:54:45Z</ExpiryTime>
                    <Expires>False</Expires>
                    <UsageCount>4</UsageCount>
                    <LocationChanged>2021-01-30T13:56:10Z</LocationChanged>
                </Times>
                <IsExpanded>True</IsExpanded>
                <DefaultAutoTypeSequence></DefaultAutoTypeSequence>
                <EnableAutoType>null</EnableAutoType>
                <EnableSearching>null</EnableSearching>
                <LastTopVisibleEntry>AAAAAAAAAAAAAAAAAAAAAA==</LastTopVisibleEntry>
            </Group>
            <Group>
                <UUID>OHEafkw6mE+lWt/WkXhvAA==</UUID>
                <Name>Internet</Name>
                <Notes></Notes>
                <IconID>1</IconID>
                <Times>
                    <CreationTime>2021-01-30T13:55:13Z</CreationTime>
                    <LastModificationTime>2021-01-30T13:55:13Z</LastModificationTime>
                    <LastAccessTime>2021-01-30T13:56:00Z</LastAccessTime>
                    <ExpiryTime>2021-01-30T13:54:45Z</ExpiryTime>
                    <Expires>False</Expires>
                    <UsageCount>3</UsageCount>
                    <LocationChanged>2021-01-30T13:56:08Z</LocationChanged>
                </Times>
                <IsExpanded>True</IsExpanded>
                <DefaultAutoTypeSequence></DefaultAutoTypeSequence>
                <EnableAutoType>null</EnableAutoType>
                <EnableSearching>null</EnableSearching>
                <LastTopVisibleEntry>AAAAAAAAAAAAAAAAAAAAAA==</LastTopVisibleEntry>
            </Group>
            <Group>
                <UUID>gCfvaEJJcUu/nl/WEex9Xg==</UUID>
                <Name>eMail</Name>
                <Notes></Notes>
                <IconID>19</IconID>
                <Times>
                    <CreationTime>2021-01-30T13:55:13Z</CreationTime>
                    <LastModificationTime>2021-01-30T13:55:13Z</LastModificationTime>
                    <LastAccessTime>2021-01-30T13:56:02Z</LastAccessTime>
                    <ExpiryTime>2021-01-30T13:54:45Z</ExpiryTime>
                    <Expires>False</Expires>
                    <UsageCount>2</UsageCount>
                    <LocationChanged>2021-01-30T13:55:13Z</LocationChanged>
                </Times>
                <IsExpanded>True</IsExpanded>
                <DefaultAutoTypeSequence></DefaultAutoTypeSequence>
                <EnableAutoType>null</EnableAutoType>
                <EnableSearching>null</EnableSearching>
                <LastTopVisibleEntry>AAAAAAAAAAAAAAAAAAAAAA==</LastTopVisibleEntry>
            </Group>
            <Group>
                <UUID>AqDlSirWj0CKkEmv2Suu1A==</UUID>
                <Name>Homebanking</Name>
                <Notes></Notes>
                <IconID>37</IconID>
                <Times>
                    <CreationTime>2021-01-30T13:55:13Z</CreationTime>
                    <LastModificationTime>2021-01-30T13:55:13Z</LastModificationTime>
                    <LastAccessTime>2021-01-30T13:56:02Z</LastAccessTime>
                    <ExpiryTime>2021-01-30T13:54:45Z</ExpiryTime>
                    <Expires>False</Expires>
                    <UsageCount>1</UsageCount>
                    <LocationChanged>2021-01-30T13:55:13Z</LocationChanged>
                </Times>
                <IsExpanded>True</IsExpanded>
                <DefaultAutoTypeSequence></DefaultAutoTypeSequence>
                <EnableAutoType>null</EnableAutoType>
                <EnableSearching>null</EnableSearching>
                <LastTopVisibleEntry>AAAAAAAAAAAAAAAAAAAAAA==</LastTopVisibleEntry>
            </Group>
            <Group>
                <UUID>0R58j2/XOUyr35WoO0xvLA==</UUID>
                <Name>Netzwerk</Name>
                <Notes></Notes>
                <IconID>3</IconID>
                <Times>
                    <CreationTime>2021-01-30T13:55:13Z</CreationTime>
                    <LastModificationTime>2021-01-30T13:55:13Z</LastModificationTime>
                    <LastAccessTime>2021-01-30T13:55:58Z</LastAccessTime>
                    <ExpiryTime>2021-01-30T13:54:45Z</ExpiryTime>
                    <Expires>False</Expires>
                    <UsageCount>3</UsageCount>
                    <LocationChanged>2021-01-30T13:55:58Z</LocationChanged>
                </Times>
                <IsExpanded>True</IsExpanded>
                <DefaultAutoTypeSequence></DefaultAutoTypeSequence>
                <EnableAutoType>null</EnableAutoType>
                <EnableSearching>null</EnableSearching>
                <LastTopVisibleEntry>AAAAAAAAAAAAAAAAAAAAAA==</LastTopVisibleEntry>
            </Group>
            <Group>
                <UUID>FJl8pvLMM0Wb2SFJ0qtn5Q==</UUID>
                <Name>Windows</Name>
                <Notes></Notes>
                <IconID>38</IconID>
                <Times>
                    <CreationTime>2021-01-30T13:55:13Z</CreationTime>
                    <LastModificationTime>2021-01-30T13:55:13Z</LastModificationTime>
                    <LastAccessTime>2021-01-30T13:55:59Z</LastAccessTime>
                    <ExpiryTime>2021-01-30T13:54:45Z</ExpiryTime>
                    <Expires>False</Expires>
                    <UsageCount>3</UsageCount>
                    <LocationChanged>2021-01-30T13:55:59Z</LocationChanged>
                </Times>
                <IsExpanded>True</IsExpanded>
                <DefaultAutoTypeSequence></DefaultAutoTypeSequence>
                <EnableAutoType>null</EnableAutoType>
                <EnableSearching>null</EnableSearching>
                <LastTopVisibleEntry>AAAAAAAAAAAAAAAAAAAAAA==</LastTopVisibleEntry>
            </Group>
            <Group>
                <UUID>LG2lFhzI3kGiWLqst+JbWg==</UUID>
                <Name>Papierkorb</Name>
                <Notes></Notes>
                <IconID>43</IconID>
                <Times>
                    <CreationTime>2021-01-30T13:55:51Z</CreationTime>
                    <LastModificationTime>2021-01-30T13:55:51Z</LastModificationTime>
                    <LastAccessTime>2021-01-30T13:56:05Z</LastAccessTime>
                    <ExpiryTime>2021-01-30T13:54:45Z</ExpiryTime>
                    <Expires>False</Expires>
                    <UsageCount>5</UsageCount>
                    <LocationChanged>2021-01-30T13:56:07Z</LocationChanged>
                </Times>
                <IsExpanded>True</IsExpanded>
                <DefaultAutoTypeSequence></DefaultAutoTypeSequence>
                <EnableAutoType>false</EnableAutoType>
                <EnableSearching>false</EnableSearching>
                <LastTopVisibleEntry>AAAAAAAAAAAAAAAAAAAAAA==</LastTopVisibleEntry>
            </Group>
        </Group>
        <DeletedObjects />
    </Root>
</KeePassFile>

Vielen Dank
 
in python wäre es:

Python:
from lxml import etree

xml = etree.parse('c:/bla.xml')

for node in xml.xpath('//History/Entry/String/Key[text()="Notes"]/../Value'):
    node.getparent().remove(node)

xml.write('c:/bla_new.xml', pretty_print=True)

vielleicht hilft dir ja schon der xpath weiter.
 
Und in Ps ist es SelectNodes. Das andere Ding heißt Single weil EIN Knoten. Nicht Viele Knoten.

Der xpath ist derselbe.
 
@0x8100 Soeben Python 3.9 aus dem Store installiert. Beim Aufruf meint er, dass das Modul lxml nicht gefunden wurde.

Ich habe das mal versucht zu adaptieren:
PowerShell:
[CODE][xml]$xml = (Get-Content -Path C:\test\Datenbank.xml -Encoding utf8 -raw)
$xml.DocumentElement.SelectNodes('//History/Entry/String/Key[text()="Notes"]/../Value')

foreach ($element in $test)
{
    $ersetzt = $element.InnerText.Replace("*","")
    $ersetzt
}

So ich gebe mir die ganzen Werte aus, die ersetzt werden sollen. Ich müsste ja irgendwie sagen, dass er dann $xml neu schreiben soll, wobei ich ja nur $ersetzt habe, die nur während der Schleife vorhanden ist.
 
Zuletzt bearbeitet:
wenn du den value auf einen leeren wert setzen willst, dann ist doch nur sowas?

PowerShell:
[xml]$xml = (Get-Content -Path c:\bla.xml -Encoding utf8 -raw)
foreach ($node in $xml.DocumentElement.SelectNodes('//History/Entry/String/Key[text()="Notes"]/../Value')) {
    $node.InnerText = ""
}
$xml.Save('c:\bla_new.xml')
 
funktionert, Top!
Der Output hat zwar kein Pretty line Print, ist aber egal =)

Vielen Dank
 

Ähnliche Themen

Zurück
Oben