Convert tab separated text to non-trivial xml. (pwsafe --> KeePassx)

I'd like to take the output of `pwsafe --exportdb > database.txt` and convert it to a KeePassX XML friendly format (feature request in pwsafe).

I found flat file converter but the syntax is beyond me with this example. Solutions are welcomed.

More details
Here is the pwsafe --> KeePassX XML translations. The pwsafe export is simply a txt file with 6 fields (the first field can be ignored):

uuid= doesn't translate
group= group>title
name= entry>title
login= entry>username
passwd= entry>password
notes= entry>comment

Example txt file for conversion (exported from pwsafe):

# passwordsafe version 2.0 database"
uuid	group	name	login	passwd	notes
"123d9-daf-df-3423423"	"retail"	"amazon"	"myamazonuser"	"sjfJ849"	"superfluous comment"
"4599d934-dsfs-324"	"retail"	"netflix"	"netflixuser"	"dj3W$#"	""
"4kdfkd-434-jj"	"email"	"gmail"	"mygmail"	"dfkpass"	""

Example xml in keepassx xml:

<!DOCTYPE KEEPASSX_DATABASE>
<database>
 <group>
  <title>Internet</title>
  <entry>
   <title>github</title>
   <username>githubusername</username>
   <password>githubpassword</password>
   <comment>optional comment</comment>
  </entry>
 </group>
 <group>
  <title>retail</title>
  <entry>
   <title>amazon</title>
   <username>username</username>
   <password>myamazonpw</password>
  </entry>
 </group>
 <group>
  <title>retail</title>
  <entry>
   <title>netflix</title>
   <username>username</username>
   <password>mynfxpw</password>
  </entry>
 </group>
</database>

Create a file named template with this content:

--HEADER--
<!DOCTYPE KEEPASSX_DATABASE>
<database>
--ROW--
  <group>
    <title>%%FIELD#02%%</title>
    <entry>
       <title>%%FIELD#03%%</title>
       <username>%%FIELD#04%%</username>
       <password>%%FIELD#05%%</password>
       <comment>%%FIELD#06%%</comment>
    </entry>
  </group>
--FOOTER--
</database>

Then you can use this code:

awk -F\" '
FNR==NR&&/^--/ {section++; next}
FNR==NR{block[section]=(block[section]?block[section]"\n":"") $0;next}
FNR==1{print block[1]}
$1 != "uuid" && ! /^#/ { out=block[2]
  for(i=1;i<=NF;i++)
     gsub(sprintf("%%%%FIELD#%02d%%%%",i),$(i*2),out);
  print out
}
END{print block[3]}' template database.txt

Edit: Questions

Could the data contain characters needing to be escaped in XML (namely double quotation mark, ampersand, apostrophe, less-than or greater-than signs)?
If the data did contains double quotation marks how would they appear in the datbase.txt file?

Thanks for the reply. It turns out that the problem was more complex than I indicated due to the need to sort and group like entries. To illustrate, take the example I provided above: the two "retail" groups should appear together in the generated xml.

Someone was kind enough to create a python script that does this replying to my post (way over my head): https://github.com/mauritiusdadd/pwsafe2keepassx