XML Replace

  • Dominik Reichl

    Dominik Reichl - 2013-12-06


    there have been many requests for search-and-replace functionalities. For example, replace something by something else in the note fields of all entries, delete entry strings by name, etc. The usual advice for such tasks is to export the database to an XML file, perform the task, and re-import the XML file. Obviously, this isn't very convenient, and is also not good from a security point of view (temporary plain text XML file stored on disk).

    Theoretically one could add each of these features. The problem is that we then would have a menu with dozens of commands; not very elegant. Thus I thought about how all these search-and-replace tasks could be realized using just one feature.

    For KeePass 2.25 I've added: XML Replace. This feature basically creates a KDBX XML DOM of the current database in memory, performs the operation specified by the user, tries to load the modified XML, and merges the current database with the modified database.

    XML Replace operates on the KDBX XML representation of a database. Nodes can be selected using an XPath expression. Available operations are removing nodes and replacing data. Data replacement can operate on the inner XML, inner text and outer XML, and regular expression replacements are supported.

    Clearly, as XML, XPath and regular expressions are involved, XML Replace is a feature for experts (and this is indicated in both the XML Replace dialog and the help page). Sample parameters for XML Replace can be found on the help page.

    Here's the latest development snapshot for testing:

    Help page:

    Every feedback is welcome. The help page currently mentions just a few examples; if you have other ideas for search-and-replace operations, please let me know and I'll add a usage example with the required parameters.

    One thing that I currently don't see how it could be realized nicely is a visual confirmation for each replacement operation.

    Thanks and best regards,

    • Bertrand Gillert (LocaSoft)


      I used another password manager (eWallet) before which could not export notes (or other fields) with newline/paragraph markers correctly. So i'd replaced them by äää. I.e. until now, all newline/paragraph markers in my KeePass DB are represented by äää. I've tried XML Replace after importing my eWallet DB using eWalletLiberatedDataImporter (which is really great as eWallet data is not structured at all). But replacing äää by \n does not work (RegEx checkbox enabled), KeePass always inserts "\n" (literal). Is this a bug? How can I replace any string by a newline (or paragraph) marker?

      Best regards from Germany,

      • Dominik Reichl

        Dominik Reichl - 2014-12-13

        Character escapes are not allowed in regular expression replacement strings, see

        One solution to your task is to replace XML (instead of text). I'd recommend the following XML Replace configuration:

        Select nodes: //Entry/String[Key='Notes']/Value
        Action: Replace data
        Data: Inner XML
        Find what: äää
        Replace with: 

        Not case-sensitive and no regular expressions.

        Best regards,

  • AlexVallat

    AlexVallat - 2013-12-07

    That's pretty cool, and the sort of feature I would use. Have you considered providing a slightly friendlier wrapper around this, though, for people who would really rather not learn XPath or look up the structure of the KDBX XML file? Some sort of wizard or simple UI that would cover 90% of the cases could generate the xpath easily enough. I'd suggest that changing the value of a node or removing a node known to be optional is considerably less dangerous than allowing arbitrary replacement of OuterXML or InnerXML, something which should really, really be left to experts!

    How about the following as steps, where you can choose to stop after any step:

    1. Select Groups: (All/(Name Is/Starts With/Ends With/Contains))
    2. Select Entries: (All/(Title/Username/Url/Notes Is/Starts With/...etc.)/
      Custom Field(Name Is/Starts...etc.)/(Value Is/Starts...etc.)
    3. Select Attachment / Field Value / Select Custom Field Name: (as above)

    Once you'd defined the selection that you want (eg: Groups[All],Entries[All],Field Value[Url]), or (Groups[Name Start With "temp"],Entries[All]) then you would have be able to either do a search and replace on the logical value of the node, or remove the node. I'd suggest showing a warning if removing a group node as it might not be immediately obvious that this will also remove the entries in that group.

    This wouldn't cover some of the more esoteric cases like changing the background colours, or icons, but would probably be 90% of what users are likely to want through search and replace.

    Of course, none of this would preclude using the full-on advanced-mode bork-the-xml-at-your-own-risk tool that you have now, I just think there's a useful gap between not being able to do anything, and being able to perform unsafe surgery on the XML like that.


  • Dominik Reichl

    Dominik Reichl - 2013-12-08

    I will consider adding such a wizard in a future version. Initially I thought about this, but found that such a wizard is not powerful enough. But as additional feature it would indeed be nice.

    As XML Replace might be useful for plugins, I've now made it available through XmlUtil.Replace. This way plugins can easily provide complex search-and-replace operations (e.g. through wizards).

    Here's the latest development snapshot for testing:

    Thanks and best regards,

    • fazthebro

      fazthebro - 2013-12-15

      Is it possible to have the replace option to include a random password? We do bulk edits of passwords for users which is currently impossible in Keepass.

  • Paul

    Paul - 2013-12-16

    There is no way to do multiple random replaces in one go, but you can do it in "number of entries to change" + 1 steps - you have to provide the random passwords yourself.
    The first step would be to replace all existing passwords with a standard value.
    Then you would perform a search and replace "number of entries" times, where the match would be the first node that has the standard value and the replace is your provided password.

    I don't think the feature provides return values, so knowing how many entries to change would require you finding this information. Given this complexity I would suggest you write a plug-in.

    Dominik, can you perform multiple search/replace operations before the DOM merge? This would allow this sort of activity without generating "false" history entries.

    cheers, Paul

  • Dominik Reichl

    Dominik Reichl - 2013-12-16

    Currently every search-and-replace operation is coupled with a sync, but I'll think about adding your suggestion in a future version, thanks!

    Best regards,


Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

No, thanks