advait has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,
I need help in parsing a xml document.
experiments total-hybs="629" total-samples="733" total="21" to="21" fr +om="1"> &#8722; <experiment accnum="E-GEOD-8918" array="Agilent Human Genome CGH M +icroarray 44B [G4410B]" biosamples-png="download/mageml/E-GEOD-8918.b +iosamples.png" biosamples-svg="download/mageml/E-GEOD-8918.biosamples +.svg" fgem="download/mageml/E-GEOD-8918.processed.zip" fgem-count="87 +" hybs="87" id="1636341195" name="CGH profiling of 87 indolent non-ho +dgkin s lymphoma (NHL)" releasedate="2008-06-19" samples="174" sdrf +="download/mageml/E-GEOD-8918.sdrf.txt" species="Homo sapiens" two-co +lumns="download/mageml/E-GEOD-8918.2columns.txt"> &#8722; <users> <user id="1"/> </users> &#8722; <secondaryaccessions> <secondaryaccession>GSE8918</secondaryaccession> </secondaryaccessions> &#8722; </experiment>
Can anyone please suggest how can I retrieve accnum in experiment. Using XML twig I could retrieve secondaryaccession.
perl script is as follow
use strict; use XML::Twig; my $t= new XML::Twig( TwigHandlers=> { experiment => \&experiment}); $t->parsefile( '/home/adi/lwp_cgh_hs.xml'); exit; sub experiment { my ($t, $experiment)= @_; my %experiment; $experiment{secondaryaccessions}= $experiment->field( 'secondaryac +cessions'); # $experiment{name}= $experiment->field( 'experimentName'); #$experiment{description}= $experiment->field( 'Description'); $experiment{secondaryaccession}= join ':', map {$_->text || ''} @{[$experiment->children( 'secondaryaccession' +)]}; print "$experiment\n"; print "$experiment{secondaryaccessions}: \n"; print " : $experiment{secondaryaccession}\n"; $experiment->delete; }
Thank you

Replies are listed 'Best First'.
Re: XML parsing
by toolic (Bishop) on Jul 07, 2008 at 16:29 UTC
    Can anyone please suggest how can I retrieve accnum in experiment.
    Here is one way to do it, after cleaning up what I think is the XML you want:
    use strict; use warnings; use XML::Twig; my $xfile = <<EOF; <experiment accnum="E-GEOD-8918" array="Agilent Human Genome CGH Micro +array 44B [G4410B]" biosamples-png="download/mageml/E-GEOD-8918.biosa +mples.png" biosamples-svg="download/mageml/E-GEOD-8918.biosamples.svg +" fgem="download/mageml/E-GEOD-8918.processed.zip" fgem-count="87" hy +bs="87" id="1636341195" name="CGH profiling of 87 indolent non-hodgki +n s lymphoma (NHL)" releasedate="2008-06-19" samples="174" sdrf="do +wnload/mageml/E-GEOD-8918.sdrf.txt" species="Homo sapiens" two-column +s="download/mageml/E-GEOD-8918.2columns.txt"> <users> <user id="1"/> </users> <secondaryaccessions> <secondaryaccession>GSE8918</secondaryaccession> </secondaryaccessions> </experiment> EOF my $t= new XML::Twig(); $t->parse($xfile); my $experiment = $t->root(); print 'accnum = ', $experiment->att('accnum'), "\n";

    prints:

    accnum = E-GEOD-8918
Re: XML parsing
by Your Mother (Archbishop) on Jul 08, 2008 at 00:21 UTC

    And one more since I've become the chapter secretary for the XML::LibXML fan club (I like Twig too).

    use strict; use warnings; use XML::LibXML; my $doc = XML::LibXML->new->parse_fh(\*DATA); for my $node ( $doc->findnodes('//experiment[@accnum]') ){ print $node->getAttribute("accnum"), "\n"; } __DATA__ <?xml version="1.0" encoding="UTF-8"?> <experiments total-hybs="629" total-samples="733" total="21" to="21" f +rom="1"> <experiment accnum="E-GEOD-8918" array="Agilent Human Genome CGH Mic +roarray 44B [G4410B]" biosamples-png="download/mageml/E-GEOD-8918.bio +samples.png" biosamples-svg="download/mageml/E-GEOD-8918.biosamples.s +vg" fgem="download/mageml/E-GEOD-8918.processed.zip" fgem-count="87" +hybs="87" id="1636341195" name="CGH profiling of 87 indolent non-hodg +kin s lymphoma (NHL)" releasedate="2008-06-19" samples="174" sdrf="do +wnload/mageml/E-GEOD-8918.sdrf.txt" species="Homo sapiens" two-column +s="download/mageml/E-GEOD-8918.2columns.txt"> <users> <user id="1"/> </users> <secondaryaccessions> <secondaryaccession>GSE8918</secondaryaccession> </secondaryaccessions> </experiment> </experiments>
Re: XML parsing
by Anonymous Monk on Jul 07, 2008 at 16:29 UTC
    #!/usr/bin/perl -- use strict; use warnings; use XML::Twig; my $t = new XML::Twig( TwigHandlers=> { experiment => sub { use Data::Dumper; print join $/, @_,$_,$/; print $_->{att}->{accnum},$/; print $_->{'att'}->{'accnum'},$/; print $_->att('accnum'),$/; print $_->_dump; } } ); my $xml = q~<experiment accnum="E-GEOD-8918"> <users><user id="1"/></users> </experiment>~; $t->parse($xml); __END__ XML::Twig=HASH(0x19140c0) XML::Twig::Elt=HASH(0x1ea55b8) XML::Twig::Elt=HASH(0x1ea55b8) E-GEOD-8918 E-GEOD-8918 E-GEOD-8918 |-experiment accnum="E-GEOD-8918" | |-users | | |-user id="1"
A reply falls below the community's threshold of quality. You may see it by logging in.