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

hi,

I am a newbee to perl. I have written some simple programs but nothing very complex. I have a requirement where I have to write a complex program in Perl.

I have a XML file which looks like this :

<?xml version="1.0"> <escinfo> <dbf> <progress> <record> <title>11111</title> <id>abcd</id> </record> <record> <title>22222</title> <id>efgh</id> </record> <dummy_record> <dummy> <title>22222</title> <id>efgh</id> </dummy> </dummy_record> <dummy_record/> </progress> </dbf> </escinfo>
I want to remove the element "dummy_record" from the file and again write the output as xml. So finally the file should look like this:
<?xml version="1.0"> <escinfo> <dbf> <progress> <record> <title>11111</title> <id>abcd</id> </record> <record> <title>22222</title> <id>efgh</id> </record> </progress> </dbf> </escinfo>
How can I do this using perl ?

Thanks in advance.

Janitored by Arunbear - added code tags, as per Monastery guidelines

Replies are listed 'Best First'.
Re: Parsing XML file and removing the element
by borisz (Canon) on Apr 10, 2005 at 13:56 UTC
Re: Parsing XML file and removing the element
by Tanktalus (Canon) on Apr 11, 2005 at 03:38 UTC

    If you actually want to do this as part of a larger system where running xml_grep would be a lesser-desired choice, XML::Twig is actually not that hard to use, once you get around its huge documentation ;-). You can also read some better docs on its website. Anyway, once you've loaded your XML file in to memory, you can just find the twig you're interested in (see get_xpath), and then just tell the object to "cut()" itself out, and you're done. You just need to print out the resultant XML to wherever you want.

    use XML::Twig; my $xml = XML::Twig->new(); $xml->parsefile('foo.xml'); foreach my $el ($xml->get_xpath('dummy_record')) { $el->cut(); } $xml->print(\*STDOUT);
Re: Parsing XML file and removing the element
by gsiems (Deacon) on Apr 14, 2005 at 18:20 UTC
    This won't going to work for nested dummy_records, but you could try something like:
    while (<DATA>) { print unless ((/<dummy_record>/../<\/dummy_record>/) || (/<dummy_re +cord\/>/)); } __DATA__ <?xml version="1.0"> <escinfo> <dbf> <progress> <record> <title>11111</title> <id>abcd</id> </record> <record> <title>22222</title> <id>efgh</id> </record> <dummy_record> <dummy> <title>22222</title> <id>efgh</id> </dummy> </dummy_record> <dummy_record/> </progress> </dbf> </escinfo>