-3

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?

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.
infinitloop
  • 2,863
  • 7
  • 38
  • 55
  • 1
    Why didn't you try such a simple thing before asking? – pavel Nov 11 '11 at 19:45
  • of course i tried it before posting question. – infinitloop Nov 11 '11 at 19:52
  • 1
    Well, it appears `mytemp.txt` contains some extra text after the XML document. Most XML parsers don't like that. – cjm Nov 11 '11 at 19:54
  • @rashid: If you have tried it before posting, why didn't you provide the code and error message in the first place?
    The error you're providing has nothing to do with filename extentions but malformed XML-content.
    – pavel Nov 11 '11 at 19:57
  • @cjm, you wont believe it, there was a (tick `) in my file and i couldn't see it. its ok now. Thanks to those who actually HELPED!!! – infinitloop Nov 11 '11 at 19:57
  • 1
    @rashid: You seen to think that some comments are no help; I think you're missing a vital point here: people who are commenting are willing to help you, otherwise they wouldn't bother in the first place. Comments about missing information or unclear questions should trigger YOU to be more informative and think a bit longer about which question you want to ask and how you can get help the fastest. Remarks like the above are not really constructive... – pavel Nov 11 '11 at 20:08
  • @pavel, totally agree with you. But my level of frustration had grown significantly and now that i am calmed down i realize my mistake. – infinitloop Nov 11 '11 at 22:00
  • @rashid: no worries, we've all felt the same when we're stuck :) – pavel Nov 11 '11 at 22:20

2 Answers2

2

Yep. XML::Simple doesn't care about the file extension, only the content.

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' => {}
              }
    };
Wes Hardaker
  • 21,735
  • 2
  • 38
  • 69
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.

flesk
  • 7,439
  • 4
  • 24
  • 33