Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parse XML file with xml::twig 1

Status
Not open for further replies.

leemalloy

MIS
Mar 28, 2013
3
US
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>
 
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";
    [b] print 'DESCRIPTION = ' . $dr->first_child('DETAILS')->first_child('DEPENDENCY_FILE')->field('DESCRIPTION'), "\n";[/b]

}


"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
 
Hi

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

[navy]Like this post?[/navy]
[tab][tab][navy]Star it![/navy]

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

Feherke.
feherke.github.io
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top