in reply to Re^2: XML parsing
in thread XML parsing
<library> <book> <book1>Book1</book1> <title>Title of Book 1</title> <genre>Fantasy</genre> </book> <book> <book2>Book2</book2> <title>Not the Title of Book 1</title> <genre>Fantasy</genre> </book> </library>
use strict; use warnings; use XML::Twig; use Data::Dumper; my $DATA = ' <library> <book> <book1>Book1</book1> <title>Title of Book 1</title> <genre>Fantasy</genre> </book> <book> <book2>Book2</book2> <title>Not the Title of Book 1</title> <genre>Fantasy</genre> </book> </library> '; my $source_twig = XML::Twig->new('pretty_print' => 'indented'); $source_twig->safe_parse($DATA); foreach my $book ($source_twig->root->children('book')) { if ($book->first_child('title')->text() ne 'Title of Book 1') { $book->cut() } } $source_twig->print();
<library> <book> <book1>Book1</book1> <title>Title of Book 1</title> <genre>Fantasy</genre> </book> </library>
|
|---|