Perl: How to return attributes using XML::Twig

I'm trying to extract a subset of the data stored within a configuration export, xml file

I think that XML::Twig is the tool for the job, however, my xml file actually has the information required within attributes rather than elements

I found something in the Perl CookBook which is close to what I need, however, I can't work out how to return attributes. I have read about the att argument, but it's not clear to me how I can implement it

I'm not a Perl programmer, and I've never utilised a Perl module before so I'm struggling with the syntax using trial and error, other resources and some shell scripting experience...

The current version of the script that I've been playing with is:


use strict;
use warnings;

use XML::Twig;

my $twig = XML::Twig->new( twig_roots => { '/package/Server' => \&do_autos });
$twig->parsefile("full.xml");
$twig->purge();

sub do_autos{
      my($Autos) = $_->find_nodes("Autos");
      print $Autos->text, "\n";
}


Unfortunately, this is no good as all the interesting information is contained within the attribute - The following excerpt demonstrates this:

<?xml version="1.0" encoding="UTF-8"?>
<package>
<Server Name="NAME" Version="7.70">
   <Autos>
      <Auto ConditionBlock="true" DeclareBlock="" GroupName="Triggers" TriggerName="some trigger" BindName="tt" DebugEnabled="0" TriggerKind="2" CommentBlock="some comment" CodeBlock="some code" TriggerPriority="5" IsEnabled="0"><TriggerGroup GroupName="Redundant_Triggers" IsEnabled="0"/><TemporalTrig Frequency="5" TriggerName="AA_GenericClear_Populate_Problems_Table"/></Auto>
</Server>
</package>


Many thanks in advance for any help
marcusjonesAsked:
Who is Participating?
 
ozoCommented:
Or did you want
      print $Autos->sprint,"\n";
0
 
ozoCommented:
Did you want

<?xml version="1.0" encoding="UTF-8"?>
<package>
<Server Name="NAME" Version="7.70">
   <Autos> Text
      <Auto ConditionBlock="true" DeclareBlock="" GroupName="Triggers" TriggerName="some trigger" BindName="tt" DebugEnabled="0" TriggerKind="2" CommentBlock="some comment" CodeBlock="some code" TriggerPriority="5" IsEnabled="0"><TriggerGroup GroupName="Redundant_Triggers" IsEnabled="0"/><TemporalTrig Frequency="5" TriggerName="AA_GenericClear_Populate_Problems_Table"/></Auto>
</Autos>
</Server>
</package>
0
 
marcusjonesAuthor Commented:
Sorry, I missed the closing Autos tag in the original excerpt. What I'm after is to take an xml file like this:

<?xml version="1.0" encoding="UTF-8"?>


   
     
   



And produce a new file, or output containing the values for the following attributes:

TriggerName="some trigger"
IsEnabled="0"
CodeBlock="some code"


Sprint produces the output that I need. Having referred back to the XML::Twig documentation I can now see that it says "methods like print or sprint will return the comments in the text. Using text or field on the other hand will not."

Now that I've got some values returned I'll be able to slice and dice as required

Many thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.