Developer Developer - 2 months ago 17
Perl Question

Get a Xml Tag value from regular expression in Perl

I have xml in which i have tag

<test>Value</test>
.I want to get the value of the tag.I want to do it with Perl Regular Expression
Below is my xml sample :

<?xml version="1.0"?>
<t_volume>
<test>Value</test>
<info>
<info_name>FZGA34177.b1</info_name>
<center_project>4085729</center_project>
<base_file>SETARIA_ITALICA/JGI/fasta/FZGA34177.b1.fasta</base_file>
</info>
</t_volume>


I want to get the value of this tag
<test>Value</test>
.I tried but i am not able to get the value .

$data = ($xml =~/<test>(.*?)<\/test>/i);


In the xml i am getting xml like also

<Test RequestId="1" RequestorId="test" ResponderId="Test">


How could i get the value of
RequestorId

Answer

The $xml =~/<test>(.*?)<\/test>/i expression can be evaluated in list context in which case it returns an array with all the captured groups. So you need to do something like that:

($data) = $xml =~/<test>(.*?)<\/test>/i;

Edit: For the second example, you can similarly extract the information if you capture it with a set of parentheses:

($RequestorId) = $xml =~ /<Test [^>]*\bRequestorId="([^"]*)"/;
Comments