I have a xml file for example,
<title> hello <name> hi </name> <street> id </street> this is xml file </title>
Here the parent node is title. I am going to extract the text inside the parent node removing the inner tags.
I have tried with the regex. But Is there any way other than using regex like, using some xml based functions to remove the tags. Note: the tag name is not known beforehand.
Hi I have tried this, I used the same xml
use XML::Simple;
use Data::Dumper;
my $simple = XML::Simple->new();
my $data = $simple->XMLin('XMLRemoval.xml');
my %oldHash = %$data; my %newHash = ();
while ( my ($key, $innerRef) = each %oldHash )
{
$newHash{$key} = @$innerRef[1];
}
foreach $key ( keys %newHash )
{
print $newHash{$key};
}
And I am getting the error : Can’t use string (” id “) as an ARRAY ref while “strict refs”
Based on your requirement, you can try this.
I have used the file provided by you in example.
we are here defining the root key contents in XML(or renaming it), you need to choose a key which will not be in your XML( i have choose root-contents).
Please go though the XML::Simple documentation, there are lot of options to tweak per your requirement.
I will leave the debug part to you for your code to check what was the error and how can solve it(which is explanatory itself) :).