I know that XML Simple will read xml files but what if i have a .cfg or .txt file but in XML format? Can I read them using xml simple?
我知道XML Simple会读取xml文件,但如果我有.cfg或.txt文件但是XML格式呢?我可以使用xml简单阅读它们吗?
Thank you.
谢谢。
Edit: Thanks for the negative votes!!!!
编辑:感谢负面投票!!!!
use XML::Simple;
use Data::Dumper;
$file = "mytemp.txt";
my $config = XMLin("mytemp.txt");
print Dumper($config);
And this is what i got:
这就是我得到的:
user ~]$ Extra content at the end of the document at /some path/SAX.pm line 64.
2 个解决方案
#1
2
Yep. XML::Simple doesn't care about the file extension, only the content.
是的。 XML :: Simple不关心文件扩展名,只关心内容。
You can always just test it out yourself quickly:
您可以随时快速测试它:
# echo "<foo><test><helloWorld/></test></foo>" > foo.cfg
# perl -e 'use XML::Simple; use Data::Dumper; print Dumper(XMLin("foo.cfg"));'
$VAR1 = {
'test' => {
'helloWorld' => {}
}
};
#2
1
As far as I can tell from the source of XML::Simple there's no validation on filename as long as it corresponds to a valid file. You can also create a file handle to read from your files and pass that to XML::Simple. So as long as you're able to open(my $fh, '<', 'yourfile.cfg')
there's no problem.
据我所知,从XML :: Simple的来源,只要它对应一个有效的文件,就不会对文件名进行验证。您还可以创建一个文件句柄来读取文件并将其传递给XML :: Simple。所以只要你能够打开(我的$ fh,'<','yourfile.cfg')就没问题了。
#1
2
Yep. XML::Simple doesn't care about the file extension, only the content.
是的。 XML :: Simple不关心文件扩展名,只关心内容。
You can always just test it out yourself quickly:
您可以随时快速测试它:
# echo "<foo><test><helloWorld/></test></foo>" > foo.cfg
# perl -e 'use XML::Simple; use Data::Dumper; print Dumper(XMLin("foo.cfg"));'
$VAR1 = {
'test' => {
'helloWorld' => {}
}
};
#2
1
As far as I can tell from the source of XML::Simple there's no validation on filename as long as it corresponds to a valid file. You can also create a file handle to read from your files and pass that to XML::Simple. So as long as you're able to open(my $fh, '<', 'yourfile.cfg')
there's no problem.
据我所知,从XML :: Simple的来源,只要它对应一个有效的文件,就不会对文件名进行验证。您还可以创建一个文件句柄来读取文件并将其传递给XML :: Simple。所以只要你能够打开(我的$ fh,'<','yourfile.cfg')就没问题了。