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

I am looking for a simple way to remove elements from an XML file by specific attributes, for example:
<journal> <full-title>Acta Biomaterialia</full-title> <alternate-title></alternate-title> <ISSN>1742-7061</ISSN> <E-ISSN></E-ISSN> <vol-year></vol-year> <iss-vol>1</iss-vol> <start-date>01-01-1974</start-date> <target> <siteID>1</siteID> <rank>200</rank> <activate></activate> </target> </journal>
I want search and remove the entire root element of journal by certain ISSN's that are in a list from another file. It seems like a simple "if found" statment, but I tried looking through the documentation for XML:Parser and XML:Simple, but found nothing that would assist me, maybe the logic is simpler than I think. Otherwise I have to go through 32,000 journals and remove them manually. Any help is appreciated.

Replies are listed 'Best First'.
Re: Using XML modules to remove XML elements
by mirod (Canon) on Aug 05, 2004 at 18:04 UTC

    Here is an XML::Twig that will be easy on your RAM (only one journal is in mem at any given time).

    #!/usr/bin/perl -w use strict; use XML::Twig; # you will actually get the list from a file my %to_remove= map { $_ => 1 } ( "1742-7061"); XML::Twig->new( twig_roots => { journal => \&journal }, pretty_print => 'indented', ) ->parse( \*DATA) # replace by parsefile( "file.xml") ->flush; sub journal { my( $t, $journal)= @_; # this works if the ISSN is a child of journal, # otherwise it might be a little more tricky. if( $to_remove{$journal->field( 'ISSN')}) { $journal->delete } else { $t->flush; } # ouput and cleanup memory } __DATA__ <bulk> <journal> <full-title>Acta Biomaterialia</full-title> <alternate-title></alternate-title> <ISSN>1742-7061</ISSN> <E-ISSN></E-ISSN> <vol-year></vol-year> <iss-vol>1</iss-vol> <start-date>01-01-1974</start-date> <target> <siteID>1</siteID> <rank>200</rank> <activate></activate> </target> </journal> <journal> <full-title>Acta Biomaterialia</full-title> <alternate-title></alternate-title> <ISSN>1743-7061</ISSN> <E-ISSN></E-ISSN> <vol-year></vol-year> <iss-vol>1</iss-vol> <start-date>01-01-1974</start-date> <target> <siteID>1</siteID> <rank>200</rank> <activate></activate> </target> </journal> </bulk>
Re: Using XML modules to remove XML elements
by johnnywang (Priest) on Aug 05, 2004 at 18:03 UTC
    The following should give what you want:
    use strict; use XML::Simple; my @list; #read in your list into this array my $infile ; #name of the file of your original xml my $outfile; # name of the output file my $parser = XML::Simple->new(rootname=>"bulk-load",noattr=>1,keyattr= +>['ISSN']); my $xml = $parser->XMLin($infile); foreach my $issn (@list){ delete $xml->{journal}->{$issn}; } $parser->XMLout($xml,outputfile=>$outfile);
Re: Using XML modules to remove XML elements
by gellyfish (Monsignor) on Aug 05, 2004 at 14:53 UTC

    Do you have the structure you have shown in a larger document, with enclosing elements? If so it would be possible to use, say, XML::DOM to find all the ISSN nodes and if they match the requirement then you can delete the parents.

    /J\

      The root element of the document is <bulk-load> the document is made up of the <journal> elements. The entire document is close to 3 MB. I am looking into XML::DOM, since I am unfamiliar with XML modules used in Perl. I appreciate your advice..thanks.

        IN that case you can do something like:

        use XML::DOM; + + my $xml = 'jj.xml'; my $parser = new XML::DOM::Parser; my $doc = $parser->parsefile($xml); + my $nodes = $doc->getElementsByTagName ("ISSN"); + foreach my $node (@{$nodes}) { if ( $node->getFirstChild()->toString() eq '1742-7061' ) { my $parent = $node->getParentNode(); $doc->getFirstChild()->removeChild($parent); } + } + print $doc->toString();
        Of course in your production code you will want to have all the ISSN numbers you want removed in a hash and do exists rather than the single comparison I have there.

        /J\

Re: Using XML modules to remove XML elements
by gmpassos (Priest) on Aug 05, 2004 at 21:23 UTC
    use XML::Smart ; my $xml = new XML::Smart(q` <root> <journal> <full-title>Acta Biomaterialia</full-title> <alternate-title></alternate-title> <ISSN>1742-7061</ISSN> <E-ISSN></E-ISSN> <vol-year></vol-year> <iss-vol>1</iss-vol> <start-date>01-01-1974</start-date> <target> <siteID>1</siteID> <rank>200</rank> <activate></activate> </target> </journal> <journal> <full-title>Acta Biomaterialia</full-title> <alternate-title></alternate-title> <ISSN>1742-7062</ISSN> <E-ISSN></E-ISSN> <vol-year></vol-year> <iss-vol>1</iss-vol> <start-date>01-01-1974</start-date> <target> <siteID>1</siteID> <rank>200</rank> <activate></activate> </target> </journal> </root> `); my @jornals = @{ $xml->{root}{journal} } ; foreach my $jornals_i ( @jornals ) { if ( $jornals_i->{ISSN} eq '1742-7061' ) { my $i = $jornals_i->i() ; delete $xml->{root}{journal}[$i] ; } } print $xml->data ;

    Graciliano M. P.
    "Creativity is the expression of the liberty".

Re: Using XML modules to remove XML elements
by Anonymous Monk on Aug 22, 2012 at 17:58 UTC
    Thanks for the Help. Very Useful! I was able to delete elements with specific attributes after dumping (Dumper()) the $xml file.
Re: Using XML modules to remove XML elements
by Maxim (Sexton) on Oct 05, 2004 at 09:46 UTC
    I found this section very useful for web developper with XML database. I give 5 votes