#!/usr/bin/perl -w use strict; use XML::Twig; my $conf = "./sample.conf.xml"; my $twig; sub parse_confxml { $twig = XML::Twig->new(keep_encoding => 1, twig_roots => { db => 1, export => 1 }); unless ($twig->safe_parsefile($conf)){ return (undef,undef,undef); } my $root = $twig->root; my $db = $root->first_child('db'); my $export = $root->first_child('export'); return ($db->att('num'), $export->att('dir'), $export->att('title')); } sub parse_confxml2 { $twig = XML::Twig->new(keep_encoding => 1, twig_roots => { db => 1, export => 1 }); unless ($twig->safe_parsefile($conf)){ return (undef,undef,undef); } my $root = $twig->root; my $db = $root->first_child('db'); my $post = $root->first_child('export')->first_child; return ($db->att('num'), $post->att('error'), $post->att('address')); } sub parse_confxml3 { $twig = XML::Twig->new(keep_encoding => 1, twig_roots => { db => 1, post => 1 }); unless ($twig->safe_parsefile($conf)){ return (undef,undef,undef); } my $root = $twig->root; my $db = $root->first_child('db'); my $post = $root->first_child('post'); return ($db->att('num'), $post->att('error'), $post->att('address')); } sub parse_confxml4 { $twig = XML::Twig->new(keep_encoding => 1, twig_roots => { db => 1, export => 1 }); unless ($twig->safe_parsefile($conf, NoExpand => 1)){ return (undef,undef,undef); } my $root = $twig->root; my $db = $root->first_child('db'); my $post = $root->first_child('export')->first_child; my @columns = $post->children('column'); return ($db->att('num'), @columns); } my ($db,$dir,$title,$error,$address,@columns); ($db,$dir,$title) = parse_confxml(); print "db=", $db, " dir=", $dir, " title=", $title, "\n"; $twig->dispose; print "\n"; ($db,$error,$address) = parse_confxml2(); print "db=", $db, " error=", $error, " address=", $address, "\n"; $twig->dispose; print "\n"; ($db,$error,$address) = parse_confxml3(); print "db=", $db, " error=", $error, " address=", $address, "\n"; $twig->dispose; print "\n"; ($db,@columns) = parse_confxml4(); print "db=", $db, "\n"; my $col; foreach $col (@columns){ print $col->att('name'), "\n"; } $twig->dispose; exit;