Click here to get back home

Help needed with XML::Smart

 HomeNewsGroups | Search | About
 comp.lang.perl.modules    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Help needed with XML::Smart Ember 09-23-2004
Posted by Ember on September 23, 2004, 9:41 am
Please log in for more thread options
Ok, so I've been working with this one for a few days - tried multiple
ways of coding around the problem, but just can't seem to get it to
work. It's not a question or issue I've seen elsewhere. I was looking
for a module to parse and write XML using Perl that didn't require a C
compiler to install. I came across XML::Smart, and loved it until I
found this issue. I'm basically using it to write out and read in a
config file that will set several variables for scripts intended for
DB2 maintenance. I need to get scalars, arrays, and hashes in from the
config file for various purposes. I'm now writing the UI to interface
with just the XML file to propmt the user for values and then put
those values in a format that can be read (again using XML::Smart) by
the Perl scripts that do database maintenance. I'm trying to both read
and write from the same file(eventually), and to have a generic UI
that can work with any XML file with a similar format. The user must
be able to add, change, or delete items in the hash variables. I had
no problem coding addition or deletion for the array variables.

The problem I'm having is that when I go to delete one of the pairs in
my hash, it leaves a blank line rather than removing the tags
entirely. I can't figure out how to get rid of that blank line (Which
shows up in the written out XML as <pair><key/><value/></pair). I've
tried every way of coding around it I can think of. Maybe I'm doing
something not so smart in my general logic - please tell me if there's
a better way to get key/value pairs in XML. Format of the XML at this
time is entirely flexible - I can change it in any way to match the
needs of this script, as long as I can get key/value pairs to work in
some way. It's interesting to note that this works if I'm deleting the
last key/value pair, but it leaves the blank line for any other
deletion. I've included almost all of my code below for context. The
problem is in these lines:
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
if ($keyValue =~ /^n$/) {
my $arrayForDeletion = $value - 1;
my $varName = $variable->;
delete($xml->->('name', 'eq',
"$varName")->[$arrayForDeletion]);
}
next;
}
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
The script's still rough in other areas, I know, but this is a core
problem I need to get around somehow before moving forward.


Here's the relevant section of my XML file:
<?xml version="1.0"?>
<cfgfile>
....
<variable name="AlarmThreshHash" type="hash">
        <prompt>If you would like custom alarm levels for specific
tablespaces, enter the tablespace name(key) and the percentage(value)
above which you would like alarms generated</prompt>
        <keyprompt>Tablespace Name</keyprompt>
<valueprompt>Percentage full for alarms</valueprompt>
        <pair><key>EXAMPLE_TS</key><value>80</value></pair>
        <pair><key>EXAMPLE_TS1</key><value>85</value></pair>
</variable>
....
</cfgfile>
And here's the perl code I'm using to parse it:
#!/usr/bin/perl -w
....
#------------------------------------------------------------------------------#
# Declare libraries/modules to be used and Global variables
#
#------------------------------------------------------------------------------#
use strict;
use Getopt::Long;
use XML::Smart;
....
#------------------------------------------------------------------------------#
# Subroutine readCfgFile
#
#------------------------------------------------------------------------------#
#

sub readCfgFile {
$xml = new XML::Smart( "$CfgFile", 'XML::Smart::Parser' );
my @TempArray = @->};
foreach my $variable (@TempArray) {
setVariable($variable);
}
} #end subroutine readCfgFile


#------------------------------------------------------------------------------#
# Subroutine setVariable
#
#------------------------------------------------------------------------------#
#
sub setVariable {
if ($^O =~ /Win/) else
my $variable = $_[0];
my $chgFlag = 0;
if ($variable-> eq "scalar"){
print "$variable->. To retain the current value of
$variable->".
", just hit enter : ";
my $value=<STDIN>;
if ($value =~ /^n$/) { $value = $variable->; }
else {chomp $value; $chgFlag = 1;}
if ($chgFlag == 1) {
$xml->->('name', 'eq',
"$variable->")-> = $value;
}
} #end if on variable type of scalar
elsif ($variable-> eq "array"){
print "$variable->.nThe current list is:n";
my $listCount = 0;
foreach (@}) {
$listCount+=1;
print " $listCount.t $_n";
} #end foreach
my $done = 0;
until ( $done == 1 ) {
print " To delete an item on this list, enter the number of that
item.n";
print " To add an item to this list, enter the item.n";
print " To accept the list as-is or to stop entering items, simply
hit enter: ";
my $value=<STDIN>;
if ($value =~ /^n$/) { $done = 1; next;}
chomp $value;
if ($value =~ /^[0-9]+$/) {
$xml->->('name', 'eq',
"$variable->")->[$value-1] = undef;
}
else {
push (@->('name', 'eq',
"$variable->")->}, $value);
}
} #end until
} #end elsif on variable type of array
elsif ($variable-> eq "hash"){
my $done = 0;
until ( $done == 1 ) {
# if ($^O =~ /Win/) else
print "$variable->.nThe current list is:n";
print " NUMBER t KEYt VALUEn";
my $listCount = 0;
foreach (@}) {
$listCount+=1;
print " $listCount.t $_->t $_->n";
} #end foreach
print " To delete a pair of data on this list, enter the number of
that pair, and follow the prompts.n";
print " To alter a value for a key, enter the number of that pair
and follow the prompts.n";
print " To add an item to this list, enter 0(zero) and follow the
prompts.n";
print " To accept the list as-is, simply hit enter: ";
my $value=<STDIN>;
if ($value =~ /^n$/) { $done = 1; next;}
chomp $value;
if ($value =~ /^[1-9]+$/) {
my $keySelect = $variable->[$value-1]->;
print " Pair selected: $variable->[$value-1]->t
$variable->[$value-1]->n";
print " To return to the previous prompt without changing this
pair, enter -1.";
print " To delete this pair, simply hit enter.n";
print " To change the value for this key, enter the new value: ";
my $keyValue =<STDIN>;
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
if ($keyValue =~ /^n$/) {
my $arrayForDeletion = $value - 1;
my $varName = $variable->;
delete($xml->->('name', 'eq',
"$varName")->[$arrayForDeletion]);
next;
}
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
chomp $keyValue;
if ($keyValue eq "-1")
else {
$xml->->('name', 'eq',
"$variable->")->[$value-1]-> = $keyValue;
}
}
elsif ($value == 0) {
print "$variable->: ";
chomp(my $key=<STDIN>);
print "$variable->: ";
chomp(my $keyValue=<STDIN>);
my $newPair = { key => "$keyt", value => "$keyValuet" };
push (@->('name', 'eq',
"$variable->")->}, $newPair);
}
} #end until
}
print "nn";
} #end subroutine set Variable


#------------------------------------------------------------------------------#
# Subroutine writeCfg
#
#------------------------------------------------------------------------------#
#
sub writeCfgFile {
$xml->data;
$xml->save('XML_cfg5.txt');
}
#------------------------------------------------------------------------------#
# Execute Subroutines
#
#------------------------------------------------------------------------------#
setupDetails;
readCfgFile;
writeCfgFile;

Any help would be greatly appreciated!


Similar ThreadsPosted
Net::FTP help needed January 15, 2007, 9:58 pm
Help needed with MakeMaker August 15, 2005, 2:37 pm
Audio DSP Info needed October 18, 2004, 11:00 pm
Perl programmer needed June 22, 2006, 2:12 am
fork process help needed March 15, 2007, 8:56 pm
Net::Telnet - Problems using "script->waitfor" - HELP Needed November 26, 2005, 1:13 am
Net::SFTP and Net::SSH::Perl hangs or disconnets. Help needed. October 25, 2004, 2:39 am
Stream filtering Module/Language needed September 24, 2007, 5:34 pm
advice needed on using proper perl graphics module(s) November 4, 2004, 1:02 pm
What module is needed to allow scripts to post to HTTPS sites? August 2, 2006, 8:06 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap