use strict;
use warnings;
use XML::LibXML;
use Text::CSV;
# Replace 'your_xml_file.xml' with the path to your XML file
my $xml_file = 'title.xml';
# Create an XML::LibXML parser
my $parser = XML::LibXML->new;
# Parse the XML file
my $doc = $parser->parse_file($xml_file);
# Find all <title> tags in the XML
my @titles = $doc->findnodes('//title');
# Create a Text::CSV object to write to a CSV file
my $csv = Text::CSV->new({ binary => 1, eol => "\n" });
# Open the CSV file for writing
open my $csv_fh, '>', 'oriental_medicine_seoul.csv' or die "Failed to open CSV file: $!";
# Write header row to the CSV file
$csv->print($csv_fh, ["Title"]);
# Extract and write the text within each <title> tag to the CSV file
foreach my $title (@titles) {
my $title_text = $title->to_literal;
$csv->print($csv_fh, [$title_text]);
}
# Close the CSV file
close $csv_fh;
# If no <title> tags are found
if (!@titles) {
print "No title tags found in the XML.\n";
} else {
print "Extraction complete. Titles saved to 'oriental_medicine_seoul.csv'.\n";
}