Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Get XML content using XML::Twig module

by kalyanrajsista (Scribe)
on Apr 21, 2010 at 11:14 UTC ( #836021=perlquestion: print w/ replies, xml ) Need Help??
kalyanrajsista has asked for the wisdom of the Perl Monks concerning the following question:

Hello all

I'm trying to parse the XML using XML::Twig Module as my XML could be very large to handle using XML::Simple. Please help me out of how to print the values based on the following...

get the values of Sender, Receiver

get the FileType. In this case possible values are InitTAP,FatalRAP,ReTxTAP

get the values of Sender, Receiver get the FileType. In this case possible values are InitTAP,FatalRAP, +ReTxTAP

Here is the XML content....

<?xml version="1.0" encoding="UTF-8"?> <Data> <ConnectionList> <Connection> <Sender>BRADD</Sender> <Receiver>SHANE</Receiver> <FileItemList> <FileItem> <FileID>378910</FileID> <Tmstp>2009-01-16T16:59:07+01:00</Tmstp> <FileType> <InitTAP> <TAPSeqNo>00083</TAPSeqNo> <NotifFileInd>false</NotifFileInd> <ChargeInfo> <TAPTxCutoffTmstp>2009-01-16T09:43:26+02:00</T +APTxCutoffTmstp> <TAPAvailTmstp>2009-01-16T16:59:07+01:00</TAPA +vailTmstp> <TAPCurrency>XDR</TAPCurrency> <TotalNoOfCalls>39</TotalNoOfCalls> <TotalNetCharge>11.470</TotalNetCharge> <TotalTax>0.000</TotalTax> </ChargeInfo> </InitTAP> </FileType> </FileItem> <FileItem> <FileID>380582</FileID> <Tmstp>2009-01-20T18:00:00+01:00</Tmstp> <FileType> <ReTxTAP> <TAPSeqNo>00083</TAPSeqNo> <NotifFileInd>false</NotifFileInd> <RefRAPSeqNo>00044</RefRAPSeqNo> <RefRAPID>380573</RefRAPID> <ChargeInfo> <TAPTxCutoffTmstp>2009-01-16T09:43:26+02:00</T +APTxCutoffTmstp> <TAPAvailTmstp>2009-01-20T18:00:00+01:00</TAPA +vailTmstp> <TAPCurrency>XDR</TAPCurrency> <TotalNoOfCalls>39</TotalNoOfCalls> <TotalNetCharge>11.470</TotalNetCharge> <TotalTax>0.000</TotalTax> </ChargeInfo> </ReTxTAP> </FileType> </FileItem> <FileItem> <FileID>380573</FileID> <Tmstp>2009-01-16T20:34:45+01:00</Tmstp> <FileType> <FatalRAP> <RAPSeqNo>00044</RAPSeqNo> <RAPStatus>Exchanged</RAPStatus> <RefTAPSeqNo>00083</RefTAPSeqNo> <RefTAPID>378910</RefTAPID> <RAPCreatTmstp>2009-01-16T20:21:30+01:00</RAPCreat +Tmstp> <RAPAvailTmstp>2009-01-16T20:21:30+01:00</RAPAvail +Tmstp> <ChargeInfo> <TAPTxCutoffTmstp>2009-01-16T09:43:26+02:00</T +APTxCutoffTmstp> <TAPAvailTmstp>2009-01-16T16:59:07+01:00</TAPA +vailTmstp> <TAPCurrency>XDR</TAPCurrency> <TotalNoOfCalls>-39</TotalNoOfCalls> <TotalNetCharge>-11.470</TotalNetCharge> <TotalTax>0.000</TotalTax> </ChargeInfo> </FatalRAP> </FileType> </FileItem> </FileItemList> </Connection> </ConnectionList> </Data>

Comment on Get XML content using XML::Twig module
Select or Download Code
Re: Get XML content using XML::Twig module
by Anonymous Monk on Apr 21, 2010 at 11:46 UTC
Re: Get XML content using XML::Twig module
by mirod (Canon) on Apr 21, 2010 at 17:26 UTC

    You could write something like the code below, which is very simple but probably not easily extensible, or write at the Connection level, have just one twig_handler for the connection, extract whichever info you need from the tree for that element, then purge it and be done. The methods used in the code below should help you write a better version.

    #!/usr/bin/perl use strict; use warnings; use XML::Twig; XML::Twig->new( twig_handlers => { Sender => sub { print "sender: ", + $_->text, "\n"; }, Receiver => sub { print "receiver: +", $_->text, "\n"; }, FileType => sub { print " file: ", + $_->first_child->tag, "\n"; $_->purge; }, }, ) ->parsefile( "fdata.xml");

      Looks quite a bit like XML::Rules :-)

      use strict; use warnings; use XML::Rules; XML::Rules->new(rules => { Sender => sub { print "sender: ", $_[1]->{_ +content}, "\n"; }, Receiver => sub { print "sender: ", $_[1]->{_content}, "\n"; }, FileType => sub { delete $_[1]->{_content}; print " file: ", (keys( +%{$_[1]}))[0], "\n"; return; }, # it would work without the following rules, but is more efficient w +ith them 'InitTAP,FatalRAP,ReTxTAP' => 'content', '_default' => '', })->parsefile( "fdata.xml");

      Jenda
      Enoch was right!
      Enjoy the last years of Rome.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://836021]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2014-08-31 03:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (294 votes), past polls