http://www.perlmonks.org?node_id=1021571

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

Replies are listed 'Best First'.
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

        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.

        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

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