in reply to Re^3: XML::Twig Help
in thread XML::Twig Help

Good day team,
I wanted to thank everyone for the suggestions and I seem to be on my way using this module but I am stuck on one final extraction component and not sure exactly what I am doing wrong.

Below is a sample of the xml and the small snipnet of code I am trying to utilize. But No Joy!

I would appreciate one final example to help me get over this last hump and I think I will be ok if I could just understand how to walk down the twig just a few elements and then be able to understand the proper way to get further down the twig.

Again many thanks in advance.

#!/home/p1aldbw1/aldb/perl-5.12.2/bin/perl use warnings; use strict; use XML::Twig; use Data::Dumper; my $xmlstr = <<EOF; <AGENT hostname="viper3"> <NAMESPACE> <APPCLASS name="ORACLE_AVAILABILITY"> <INSTANCE name="atecr"> <PARAMETER name="InstanceStatus"> <TYPE>CONSUMER</TYPE> <BORDER> <ACTIVE>0</ACTIVE> <MIN>0</MIN> <MAX>0</MAX> <TRIGGER>Immediate</TRIGGER> <RECOVERY>0</RECOVERY> <STATE>OK</STATE> </BORDER> <ALARM1> <ACTIVE>1</ACTIVE> <MIN>1</MIN> <MAX>1</MAX> <TRIGGER>After 7 times</TRIGGER> <RECOVERY>1</RECOVERY> <STATE>WARN</STATE> </ALARM1> <ALARM2> <ACTIVE>1</ACTIVE> <MIN>2</MIN> <MAX>2</MAX> <TRIGGER>After 7 times</TRIGGER> <RECOVERY>1</RECOVERY> <STATE>ALARM</STATE> </ALARM2> </PARAMETER> <PARAMETER name="RSSpaceLeft"> <TYPE>CONSUMER</TYPE> <BORDER> <ACTIVE>0</ACTIVE> <MIN>0</MIN> <MAX>0</MAX> <TRIGGER>Immediate</TRIGGER> <RECOVERY>0</RECOVERY> <STATE>OK</STATE> </BORDER> <ALARM1> <ACTIVE>0</ACTIVE> <MIN>0</MIN> <MAX>0</MAX> <TRIGGER>Immediate</TRIGGER> <RECOVERY>0</RECOVERY> <STATE>OK</STATE> </ALARM1> <ALARM2> <ACTIVE>0</ACTIVE> <MIN>0</MIN> <MAX>0</MAX> <TRIGGER>Immediate</TRIGGER> <RECOVERY>0</RECOVERY> <STATE>OK</STATE> </ALARM2> </PARAMETER> <PARAMETER name="CannotExtend"> <TYPE>CONSUMER</TYPE> <BORDER> <ACTIVE>0</ACTIVE> <MIN>0</MIN> <MAX>0</MAX> <TRIGGER>Immediate</TRIGGER> <RECOVERY>0</RECOVERY> <STATE>OK</STATE> </BORDER> <ALARM1> <ACTIVE>1</ACTIVE> <MIN>1</MIN> <MAX>1</MAX> <TRIGGER>Immediate</TRIGGER> <RECOVERY>1</RECOVERY> <STATE>ALARM</STATE> </ALARM1> <ALARM2> <ACTIVE>0</ACTIVE> <MIN>0</MIN> <MAX>0</MAX> <TRIGGER>Immediate</TRIGGER> <RECOVERY>0</RECOVERY> <STATE>OK</STATE> </ALARM2> </PARAMETER> </INSTANCE> </APPCLASS> <APPCLASS name="ORACLE_CAPACITY"> <INSTANCE name="atecq"> <PARAMETER name="FreeSpace"> <TYPE>CONSUMER</TYPE> <BORDER> <ACTIVE>0</ACTIVE> <MIN>0</MIN> <MAX>0</MAX> <TRIGGER>Immediate</TRIGGER> <RECOVERY>0</RECOVERY> <STATE>OK</STATE> </BORDER> <ALARM1> <ACTIVE>1</ACTIVE> <MIN>1</MIN> <MAX>1</MAX> <TRIGGER>After 7 times</TRIGGER> <RECOVERY>1</RECOVERY> <STATE>WARN</STATE> </ALARM1> <ALARM2> <ACTIVE>1</ACTIVE> <MIN>2</MIN> <MAX>2</MAX> <TRIGGER>After 7 times</TRIGGER> <RECOVERY>1</RECOVERY> <STATE>ALARM</STATE> </ALARM2> </PARAMETER> <PARAMETER name="ExtentsLeft"> <TYPE>CONSUMER</TYPE> <BORDER> <ACTIVE>0</ACTIVE> <MIN>0</MIN> <MAX>0</MAX> <TRIGGER>Immediate</TRIGGER> <RECOVERY>0</RECOVERY> <STATE>OK</STATE> </BORDER> <ALARM1> <ACTIVE>0</ACTIVE> <MIN>0</MIN> <MAX>0</MAX> <TRIGGER>Immediate</TRIGGER> <RECOVERY>0</RECOVERY> <STATE>OK</STATE> </ALARM1> <ALARM2> <ACTIVE>0</ACTIVE> <MIN>0</MIN> <MAX>0</MAX> <TRIGGER>Immediate</TRIGGER> <RECOVERY>0</RECOVERY> <STATE>OK</STATE> </ALARM2> </PARAMETER> <PARAMETER name="CursorsLeft"> <TYPE>CONSUMER</TYPE> <BORDER> <ACTIVE>0</ACTIVE> <MIN>0</MIN> <MAX>0</MAX> <TRIGGER>Immediate</TRIGGER> <RECOVERY>0</RECOVERY> <STATE>OK</STATE> </BORDER> <ALARM1> <ACTIVE>1</ACTIVE> <MIN>1</MIN> <MAX>1</MAX> <TRIGGER>Immediate</TRIGGER> <RECOVERY>1</RECOVERY> <STATE>ALARM</STATE> </ALARM1> <ALARM2> <ACTIVE>0</ACTIVE> <MIN>0</MIN> <MAX>0</MAX> <TRIGGER>Immediate</TRIGGER> <RECOVERY>0</RECOVERY> <STATE>OK</STATE> </ALARM2> </PARAMETER> </INSTANCE> </APPCLASS> </NAMESPACE> </AGENT EOF my $twig = XML::Twig->new(twig_handlers => {NAMESPACE => \&namespace}) +; $twig->parse($xmlstr); my(@NAMESPACE,$APPCLASS,$INSTANCE,$PARAMETER,$PARAM_TYPE); sub namespace { my ($t, $elt) = @_; for my $appclass ( $elt->children() ) { $APPCLASS = $appclass->att('name'); for my $instance ($appclass->children()) { $INSTANCE = $instance->att('name'); for my $parameter ($instance->children()) { $PARAMETER = $parameter->att('name'); $PARAM_TYPE = $parameter->field('TYPE'); @NAMESPACE = $APPCLASS . "~" . $INSTANCE . "~" . $PARA +METER . "~" . $PARAM_TYPE; } } } } print Dumper @NAMESPACE; print "\n\n";


I must be on the right track because it does in fact return the first branch as expected.

$VAR1 = 'ORACLE_CAPACITY~atecq~CursorsLeft~CONSUMER';

Replies are listed 'Best First'.
Re^5: XML::Twig Help
by toolic (Bishop) on Jan 10, 2011 at 16:46 UTC
    Since you didn't specify what your desired output should be, I can only guess. I suspect you want to push into your array. I also simplified the code using join.

    change:

    @NAMESPACE = $APPCLASS . "~" . $INSTANCE . "~" . $PARA +METER . "~" . $PARAM_TYPE;
    to:
    push @NAMESPACE, join '~', $APPCLASS, $INSTANCE, $PARA +METER, $PARAM_TYPE;
    Output:
    $VAR1 = 'ORACLE_AVAILABILITY~atecr~InstanceStatus~CONSUMER'; $VAR2 = 'ORACLE_AVAILABILITY~atecr~RSSpaceLeft~CONSUMER'; $VAR3 = 'ORACLE_AVAILABILITY~atecr~CannotExtend~CONSUMER'; $VAR4 = 'ORACLE_CAPACITY~atecq~FreeSpace~CONSUMER'; $VAR5 = 'ORACLE_CAPACITY~atecq~ExtentsLeft~CONSUMER'; $VAR6 = 'ORACLE_CAPACITY~atecq~CursorsLeft~CONSUMER';
    Side note: Typically, you would pass a reference to Data::Dumper:
    print Dumper(\@NAMESPACE); __END__ $VAR1 = [ 'ORACLE_AVAILABILITY~atecr~InstanceStatus~CONSUMER', 'ORACLE_AVAILABILITY~atecr~RSSpaceLeft~CONSUMER', 'ORACLE_AVAILABILITY~atecr~CannotExtend~CONSUMER', 'ORACLE_CAPACITY~atecq~FreeSpace~CONSUMER', 'ORACLE_CAPACITY~atecq~ExtentsLeft~CONSUMER', 'ORACLE_CAPACITY~atecq~CursorsLeft~CONSUMER' ];

    Side note: use "readmore" tags for large code samples: Writeup Formatting Tips

      DANG!!!!!!!!!!!!!!!

      I'm so stupid...wasn't paying attention...I guess I am in such a hurry that I couldn't see the tree because of the forest.

      Thank you for your help...I have been so focused on the twig code I just didn't catch that...I should know better.