×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Parse XML file with xml::twig

Parse XML file with xml::twig

Parse XML file with xml::twig

(OP)
How do I get the fields ID, NAME, DESCRIPTION. Below is my current code put I can't get beyond the first_child of the file.

use strict;
use warnings;
use XML::Simple;
use Data::Dumper;
use XML::Twig;

my $t=XML::Twig->new(twig_handlers => {DATA_RECORD => \&dr});
$t->parsefile('C:\\Documents and Settings\fmall\\Desktop\\garth.xml');

sub dr {
my ($t, $dr) = @_;
print $dr->first_child('ID')->text,"\n";
print $dr->first_child('NAME')->text, "\n";
#print $dr->children('DESCRIPTION')->text(), "\n";
print $dr->sibling(1,'DETAILS')->text, "\n";
# print $item->first_child('DESCRIPTION')->text(),"\n";
print "\n";
}

<main>
<DATA_RECORD>
<ID>637</ID>
<NAME>flewatchr_ntfymmscap_up_asuser</NAME>
<FOLDERID>1054</FOLDERID>
<MODIFIED>11/8/2007 5:59:51 PM</MODIFIED>
<DETAILS>
<DEPENDENCY_FILE NAME="flewatchr_ntfymmscap_up_asuser" UID="4CD4ED0C22D5">
<AGENT AGENTTYPE="REMOTE" LOGIC="" UID="C9004C97163C"/>
<DESCRIPTION>I NEED THIS INFO</DESCRIPTION>
<ATTRIBUTES>
<WATCH_SIZE>YES</WATCH_SIZE>
<VALUE>60</VALUE>
<OPERATION>STABLE</OPERATION>
<SOURCE_PATH>\\fs06\sys\misdata\xfer\backup\ntfmmsup[SYSTEM.VARIABLE.day_number].txt</SOURCE_PATH>
</ATTRIBUTES>
</DEPENDENCY_FILE>
</DETAILS>
</DATA_RECORD>
</main>

RE: Parse XML file with xml::twig

Try this...

CODE

#!/usr/bin/perl
use strict;
use warnings;
use XML::Simple;
use XML::Twig;
    
   
my $xml = '<main>
 <DATA_RECORD>
 <ID>637</ID>
 <NAME>flewatchr_ntfymmscap_up_asuser</NAME>
 <FOLDERID>1054</FOLDERID>
 <MODIFIED>11/8/2007 5:59:51 PM</MODIFIED>
 <DETAILS>
 <DEPENDENCY_FILE NAME="flewatchr_ntfymmscap_up_asuser" UID="4CD4ED0C22D5">
 <AGENT AGENTTYPE="REMOTE" LOGIC="" UID="C9004C97163C"/>
<DESCRIPTION>I NEED THIS INFO</DESCRIPTION> 
 <ATTRIBUTES>
 <WATCH_SIZE>YES</WATCH_SIZE>
 <VALUE>60</VALUE>
 <OPERATION>STABLE</OPERATION>
 <SOURCE_PATH>\\fs06\sys\misdata\xfer\backup\ntfmmsup[SYSTEM.VARIABLE.day_number].txt</SOURCE_PATH>
 </ATTRIBUTES>
 </DEPENDENCY_FILE>
 </DETAILS>
 </DATA_RECORD>
 </main> ';
     
my $t = XML::Twig->new( twig_handlers => { DATA_RECORD => \&dr});

$t->parse($xml);

sub dr {
     my ($t, $dr) = @_;
    
     print 'ID = ' . $dr->first_child('ID')->text,"\n";
     print 'NAME = ' . $dr->first_child('NAME')->text, "\n";
     print 'DESCRIPTION = ' . $dr->first_child('DETAILS')->first_child('DEPENDENCY_FILE')->field('DESCRIPTION'), "\n";

} 

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads

RE: Parse XML file with xml::twig

(OP)
Thanks it worked perfectly.

RE: Parse XML file with xml::twig

Hi

Here on Tek-Tips we used to thank for the received help by giving stars. Please click the

Like this post?
        Star it!

at the bottom of 1DMF's post. That way you both show your gratitude and indicate this thread as helpful.

Feherke.
feherke.github.io

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close