Click to See Complete Forum and Search --> : replacing stuff.....
BassMasterFlash
05-27-2004, 06:03 PM
Say i have a text file that looks like this...
<show><picture></picture><city>Houston</city><state>Texas</state><posy>115</posy><posx>106</posx><venue>Fitzgerald's</venue><guest>Lots of Guest , 10 or so Bands</guest><comments>Crazy Tony's 10 year Anniversary</comments><price>$10</price><time>9 or so PM</time><venue_url></venue_url></show>
alright now i want to grab this and get rid of all the xml tags...
now i know php has the str_replace function ..does Perl have anything like this where i could just cut out all the specified tags and replace them with spaces or anything to my liking?
i would like to turn it to something like this>>
Houston, Texas @ Fitgerald's
Lots of Guest, 10 or so Bands
Crazy Tony's 10 year Anniversary
9 or so
So i would also have to cut out all the unnecessary tags and nodes too..like >>....posy>115</posy><posx>106</posx>... somethings are now not needed..
thanks
Trey
chrisranjana
05-28-2004, 04:36 AM
$s ="<show><picture></picture><city>Houston</city><state>Texas</state><posy>115</posy><posx>106</posx><venue>Fitzgerald's</venue><guest>Lots of Guest , 10 or so Bands</guest><comments>Crazy Tony's 10 year Anniversary</comments><price>$10</price><time>9 or so PM</time><venue_url></venue_url></show>";
$s =~ s/<.*?>//g;
print $s;
this213
05-28-2004, 02:15 PM
@s = (
"<show><picture></picture><city>Houston</city><state>Texas</state><posy>115</posy><posx>106</posx><venue>Fitzgerald's</venue><guest>Lots of Guest , 10 or so Bands</guest><comments>Crazy Tony's 10 year Anniversary</comments><price>$10</price><time>9 or so PM</time><venue_url></venue_url></show>",
"<show><picture></picture><city>Houston</city><state>Texas</state><posy>115</posy><posx>106</posx><venue>Fitzgerald's</venue><guest>Lots of Guest , 10 or so Bands</guest><comments>Crazy Tony's 10 year Anniversary</comments><price>$10</price><time>9 or so PM</time><venue_url></venue_url></show>"
);
foreach $string (@s) {
$string =~ s/<.*?>/\|/g;
@array = split(/\|/,$string);
foreach $el (@array) { unless($el eq ''){
# do stuff here
print "$el";
}
print "\n";
}
gives you a little more control over the different elements.
This
BassMasterFlash
05-29-2004, 12:07 AM
Hey guys, thanks for the quick responses, I tryed the stuff provided by Chrisranjana and it is not returning anything. Is there any documentation for the function or whatever the idea is behind this so i can get a better understanding for this? As for the info provided by this213 i seem to be getting a server error. So once again just trying to find some documentation or somemore help on this matter..
So check this out say my data comes in from the text file and is presented as so..
<city>Houston</city><state>Texas</state><posy>115</posy><posx>106</posx><venue>Fitzgerald's</venue><guest>Lots of Guest , 10 or so Bands</guest><comments>Crazy Tony's 10 year Anniversary</comments><price>$10</price><time>9 or so PM</time><venue_url></venue_url>
ok..now i want to some how breeze through this data, and for every time the instance "<venue>" and "<comments>" comes up i want to replace those with "<br>" and when the "<venue>" tag comes up i want to replace that with "@". And for the rest of the xml tags, just replace those with "".
is there a definite function for this?.. and if so, hook a brother up..I dont mind doing my own work i just need a little direction..
i appreciate all the replies.
thanks again
this213
05-29-2004, 12:46 PM
As for the info provided by this213 i seem to be getting a server error.
That's just a function, not a full program, all you should really have to do is replace:
@s = (
"<show><picture></picture><city>Houston</city><state>Texas</state><posy>115</posy><posx>106</posx><venue>Fitzgerald's</venue>
<guest>Lots of Guest , 10 or so Bands</guest><comments>Crazy Tony's 10 year Anniversary</comments><price>$10</price>
<time>9 or so PM</time><venue_url></venue_url></show>",
"<show><picture></picture><city>Houston</city><state>Texas</state><posy>115</posy><posx>106</posx><venue>Fitzgerald's</venue>
<guest>Lots of Guest , 10 or so Bands</guest><comments>Crazy Tony's 10 year Anniversary</comments><price>$10</price>
<time>9 or so PM</time><venue_url></venue_url></show>"
);
with
open(FILE,"path/to/data/file");
@s = <FILE>;
close FILE;
chomp @s;
Of course, since this was written as a function - and not as a full script, you'll have to add your
shebang to it and if you're running this CGI, you need to have the line:
print "Content-type: text/html\n\n";
before you try to print anything out or it'll die with a "Premature end of script headers" error
(look in your web server's error logs).
As to searching and replacing certain strings in the data,
foreach $string (@s) {
$string =~ s/(<tag1>)|(<tag2>)|(<tag3>)/<br>/g;
$string =~ s/(?!<br>)(<.*?>)/\|/g;
@array = split(/\|/,$string);
foreach $el (@array) { unless($el eq ''){
# do stuff here
print "$el"; }
}
print "\n";
}
Put whatever tags you want to replace with <br> into tag1, tag2, and add them as shown, with a "|" in between them.
In the original search & replace statement, I added (?!<br>), which looks to make sure the tag it's about to replace is not <br>.
This code should also explain in more detail how the search and replace is being done by the function