?
Solved

Perl: How to return attributes using XML::Twig

Posted on 2009-04-25
3
Medium Priority
?
464 Views
Last Modified: 2012-05-06
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
0
Comment
Question by:marcusjones
  • 2
3 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 24234579
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
 
LVL 85

Accepted Solution

by:
ozo earned 1000 total points
ID: 24234598
Or did you want
      print $Autos->sprint,"\n";
0
 

Author Closing Comment

by:marcusjones
ID: 31574635
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question