use diagnostics; print "This Perl script replaces the content of a given element from a series of XML updategrams\n"; print "Please enter the subdirectory of the files (do not forget to escape path dividers)\n"; my $directory =; chomp($directory); print "Please enter the tagname you want to edit (just the tagname, no need for angle brackets)\n"; my $tagname = ; chomp($tagname); print "Please enter the new content you want to use as replacement\n"; my $new_content = ; chomp($new_content); ##create new substring (done here to be done only once to speed script) my $newstring = "<$tagname>$new_content<\/$tagname>"; print "New tag content will appear as $newstring\n"; my $filecon = ""; opendir(DIR, $directory) or die "can't opendir $directory: $!"; while (defined($file = readdir(DIR))) { next if $file =~ /^\.\.?$/; open(SRC, "< $directory\\$file") or die "Could not open source $directory\\$file\n"; print "Processing file $file\n"; while ( ) { chomp; #print "$_\n"; $filecon=$filecon.$_; } close SRC; #print "$filecon\n"; $start = index($filecon, "<$tagname>"); print "Start of tag is $start\n"; $end=index($filecon, "<\/$tagname>"); print "End of tag is $end\n"; ################################################# ##Amendments done to here ##Replacement scheme needed after this ################################################# #need to replace the tag content $offset = $end-$start+length($tagname)+3; substr($filecon, $start, $offset)= $newstring; #print "$filecon\n"; #manipulate the filename #$position = index($file, "."); #$filestem = substr($file,($position-1),-100); $filestem = $file; $newfilename = "$directory\\$filestem.$tagname.xml"; print "Writing new file $newfilename\n"; #write out the amended xml file open(OUT, "> $newfilename"); print OUT "$filecon"; close OUT; $newfilename = ""; $filecon = ""; } print "The routine is finished\n"; #just to keep cmd window open for message to be seen sleep 5;
It might be good enough for a quick and dirty utility at work, but I don't think it makes much sense to post it here.
Your code doesn't even begin to cover the XML spec, and you don't list any of its obvious limitations (no attributes allowed in replaced tags, no CDATA support...).
--
The XML Police
I have no problem with people who know what they are doing using regexps on consenting XML, within the privacy of their own job. If they really know what they are doing, all will be well. If they don't, then they've brought it upon themselves, they have to deal with it. I just have problems with people posting that kind of code, as it will lead people who really don't know what they're doing to think that this is a proper way to deal with XML. This would be indecent exposure if I may push the analogy a little too far ;--)
BTW, if you can't install libxml2 (on which XML::LibXML is based), then try an expat based module, preferably XML::Twig of course ;--) On Windows, XML::Parser comes pre-installed with Activestate Perl. On any sensible platform that includes a compiler, both expat and libxml2 should install easily. XML libraries are generally not over-engineered, it's just that most custom code is under-engineered, not dealing with XML but for a very limited, and usually un-specified subset of XML (no comments, no nested tags, no mixed content are common limitations).
And if you didn't like my tongue-in-cheek police reference, how about calling my original answer a "consumer report" advice to recall the original code, would that fare better with you?
...I have had real trouble trying to install libxml etc...But any XML module has overcome most of the trouble that you might be facing now for your code maintainance.
You should save this possible future time by translating your code into any basic XML (XML::Simple or so). Now that you know exactly what to expect from an XML module, you could choose wisely the best suited, (without requiring libxml). Perhaps, one that transforms every element into a hash key, that would be easy to rename... Use Data::Dumper to test your output for a while...
perlmonks.org content © perlmonks.org and Anonymous Monk, chanio, mirod, stevee
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03