CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate: Re: [0815] How can i use xml-moduls for my problem?: Edit Log



1arryb
User

Apr 16, 2009, 3:30 PM


Views: 3674
Re: [0815] How can i use xml-moduls for my problem?

Hi 0815,

Of course, you could just use a regular expression filter on the file:

Code
perl -pi -e  's/XML_SID/My Concretized XML_SID Value/' inifile.xml


But if you really need to go the xml parse route, here's a little XML::Twig program that might give you a starting point:

Code
#!/usr/bin/perl 

use strict;
use warnings;

use XML::Twig;

sub handle_cdata {
my ($twig, $ele) = @_;

# I had trouble parsing just the #CDATA tags, so I start
# a couple of levels up.
my $strval = $ele->first_child('strval');
return unless $strval;
my $cdata = $strval->first_child('#CDATA');
return unless $cdata;

if ($cdata->text eq "XML_SID") {
# Replace XML_SID meta with something else.
my $ncdata = new XML::Twig::Elt( '#CDATA', "My Concretized XML_SID value");
$ncdata->replace($cdata);
}
}

# If other tags besides 'fld's contain stuff you want to
# transform, put them here.
my $handlers = {
'fld' => \&handle_cdata
};

my $twig = new XML::Twig(twig_handlers => $handlers);
$twig->parsefile("inifile.xml");

# Change to $twig->flush if you want edit-in-place semantics. Othersise, redirect to a file from the command line.
$twig->set_pretty_print("indented");
$twig->print;


UPDATE: BTW, the XML::Twig perldoc references an excellent tutorial on the web at http://www.xmltwig.com/tutorial/. Check it out.

Cheers,

Larry


(This post was edited by 1arryb on Apr 16, 2009, 3:46 PM)


Edit Log:
Post edited by 1arryb (User) on Apr 16, 2009, 3:32 PM
Post edited by 1arryb (User) on Apr 16, 2009, 3:33 PM
Post edited by 1arryb (User) on Apr 16, 2009, 3:46 PM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives