in reply to regex on XML
Output#!/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; }
of course this is only working for XML nodes <FIELD> right now but can easily be modified. HTH.<?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>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: regex on XML
by bear0053 (Hermit) on Feb 18, 2004 at 15:38 UTC |