Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

XML element replacement

by stevee (Acolyte)
on Apr 21, 2006 at 15:16 UTC ( #544926=snippet: print w/ replies, xml ) Need Help??

Description: Project I am working on has a requirement for a quick and dirty utility to replace all the content of a given tag in a sub-directory of XML files and create amended files. This is what I have cooked up. Treated the files as text rather than XML and parsing etc. as I have had real trouble trying to install libxml etc. First posting to the Monastery, so be KIND! Kind regards all.
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 es
+cape path dividers)\n";
my $directory = <STDIN>;
chomp($directory);

print "Please enter the tagname you want to edit (just the tagname, no
+ need for angle brackets)\n";
my $tagname =<STDIN>;
chomp($tagname);

print "Please enter the new content you want to use as replacement\n";
my $new_content =<STDIN>;
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 $di
+rectory\\$file\n";
    print "Processing file $file\n";
    while (<SRC>) {
                    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;

Comment on XML element replacement
Download Code
Re: XML element replacement
by mirod (Canon) on Apr 21, 2006 at 16:18 UTC

    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

      Curious - the poster started out by saying that he had a really hard time installing libXML or somesuch. I just tried an install of XML::Simple, and guess what? unresolved prerequisites, no good.

      I hear the most XML libraries are out of control. Java-itis, bulky, over-engineered, to difficult interfaces. I haven't tried the Perl version, but hey, guess I won't be trying XML::Simple tonight, either.

      I have a need for a simple XML parse myself. A known set of tags, the same every time. Treating it like text may be an adequately grungy thing to do. It looks to me like the guy has some training, and isn't that great at Perl. Hey, like me! I like this guy.

      I never did like 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?

Re: XML element replacement
by chanio (Priest) on Apr 23, 2006 at 21:46 UTC
    Good,

    ...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...

Back to Snippets Section

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://544926]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2014-12-27 15:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls