Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

need to parse nested XML

by rajivreddy (Initiate)
on Mar 04, 2013 at 04:06 UTC ( #1021571=perlquestion: print w/ replies, xml ) Need Help??
rajivreddy has asked for the wisdom of the Perl Monks concerning the following question:

<press-release> <PromoModuleOne> <ModuleBackgroundImage>images/section_bg.png</ModuleBackground +Image> <ColorCode>BLUE</ColorCode> <LeftColoumn> <PromoImage>XXXXXX</PromoImage> <HeadingImage>XXXXXX</HeadingImage> <PromoLink>XXXXXX</PromoLink> <PromoText>XXXXXX</PromoText> </LeftColoumn> <RightColoumn> <Tabs> <TabName>XXXX</TabName> <Links> <AssetTitle>asd</AssetTitle> <AssetSource>asdasd</AssetSource> <AssetLink>asdsadasd</AssetLink> </Links> <Links> <AssetTitle>sadas</AssetTitle> <AssetSource>asdsa</AssetSource> <AssetLink>dasdas</AssetLink> </Links> </Tabs> <Tabs> <TabName>XXXX</TabName> <Links> <AssetTitle>dsfsdfds</AssetTitle> <AssetSource>dsfds</AssetSource> <AssetLink>sdfdsfsdf</AssetLink> </Links> </Tabs> </RightColoumn> </PromoModuleOne> </press-release>
Hi monks I need to extract the values of the TABNAMES and the values of the elements between the <Links> elements repetatively.Please help

Comment on need to parse nested XML
Download Code
Re: need to parse nested XML
by tmharish (Friar) on Mar 04, 2013 at 04:28 UTC

    You can do it with XML::Smart like so:

    use strict ; use XML::Smart ; my $xml = join( '', <DATA> ) ; my $xml_obj = XML::Smart->new( $xml ); $xml_obj = $xml_obj->{ 'press-release' }{ 'PromoModuleOne' }{ 'RightCo +loumn' }{ Tabs } ; foreach my $tab ( @{ $xml_obj } ) { print $tab->{ TabName }->content() ." \n" ; foreach my $links ( @{ $tab->{ Links } } ) { print $links->{ AssetTitle } . "\n" ; print $links->{ AssetSource } . "\n" ; print $links->{ AssetLink } . "\n" ; print "#" x 50 ; print "\n"; } print "*" x 50 ; print "\n"; } __DATA__ <press-release> <PromoModuleOne> <ModuleBackgroundImage>images/section_bg.png</ModuleBackground +Image> <ColorCode>BLUE</ColorCode> <LeftColoumn> <PromoImage>XXXXXX</PromoImage> <HeadingImage>XXXXXX</HeadingImage> <PromoLink>XXXXXX</PromoLink> <PromoText>XXXXXX</PromoText> </LeftColoumn> <RightColoumn> <Tabs> <TabName>XXXX</TabName> <Links> <AssetTitle>asd</AssetTitle> <AssetSource>asdasd</AssetSource> <AssetLink>asdsadasd</AssetLink> </Links> <Links> <AssetTitle>sadas</AssetTitle> <AssetSource>asdsa</AssetSource> <AssetLink>dasdas</AssetLink> </Links> </Tabs> <Tabs> <TabName>XXXX</TabName> <Links> <AssetTitle>dsfsdfds</AssetTitle> <AssetSource>dsfds</AssetSource> <AssetLink>sdfdsfsdf</AssetLink> </Links> </Tabs> </RightColoumn> </PromoModuleOne> </press-release>

    OUTPUT:

    XXXX 
    asd
    asdasd
    asdsadasd
    ##################################################
    sadas
    asdsa
    dasdas
    ##################################################
    **************************************************
    XXXX 
    dsfsdfds
    dsfds
    sdfdsfsdf
    ##################################################
    **************************************************
    
    
Re: need to parse nested XML
by rajivreddy (Initiate) on Mar 04, 2013 at 04:53 UTC
    Is there a way to do it with XML::Simple?
      Is there a way to do it XML::Simple as i am not allowed to install perl modules

        Have you read manorhce's reply from that node I referred ?

        Is there a way to do it XML::Simple as i am not allowed to install perl modules

        Maybe, but if you can't install modules, that means you can't use any code you might find on perlmonks either, so you're stuck

        If you are allowed to upload your code, you are allowed to upload modules. The fact that you are not allowed to install modules into the shared directories is irrelevant. See for example Yes, even you can use CPAN.

        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://1021571]
Approved by tmharish
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2014-08-02 01:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (53 votes), past polls