in reply to regex on XML

Greetings all,
Here is my general idea... not all that elegant I know but it does what the OP is asking... I think.
#!/usr/bin/perl -w use strict; my $incoming_xml = ' <?xml version="1.0" encoding="UTF-8"?> <TRANSACTION> <FIELDS> <FIELD KEY="user">name</FIELD> <FIELD KEY="password">pass&word</FIELD> <FIELD KEY="operation_type"><do_what></FIELD> </FIELDS> </TRANSACTION>'; print $incoming_xml; print "\n==============\n"; $incoming_xml =~ s/(<FIELD[^>]*>)(.*)(<\/FIELD>)/$1.&html_transliterat +e($2).$3/eg; print $incoming_xml; exit; sub html_transliterate{ my $in_str = shift; $in_str =~ s/&/!38/g; $in_str =~ s/</!40/g; $in_str =~ s/>/!41/g; return $in_str; }
Output
<?xml version="1.0" encoding="UTF-8"?> <TRANSACTION> <FIELDS> <FIELD KEY="user">name</FIELD> <FIELD KEY="password">pass&word</FIELD> <FIELD KEY="operation_type"><do_what></FIELD> </FIELDS> </TRANSACTION> ============== <?xml version="1.0" encoding="UTF-8"?> <TRANSACTION> <FIELDS> <FIELD KEY="user">name</FIELD> <FIELD KEY="password">pass!38word</FIELD> <FIELD KEY="operation_type">!40do_what!41</FIELD> </FIELDS> </TRANSACTION>
of course this is only working for XML nodes <FIELD> right now but can easily be modified. HTH.

Replies are listed 'Best First'.
Re: Re: regex on XML
by bear0053 (Hermit) on Feb 18, 2004 at 15:38 UTC
    thanks that is exactly what i was looking for. as usual perlmonks come through again.