By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,834 Members | 2,240 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,834 IT Pros & Developers. It's quick & easy.

Is <id> reserved and unusable in perl XML::Simple?

BeemerBiker
P: 87
I cannot get the following code to work unless I
(1) Change <id> to anything else, or
(2) Add an empty section to the bottom of my xml file.

Could this be a bug in 5.10.1 ActivePerl or my test xml script is no good?

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3. use XML::Simple;
  4. use XML::Parser;
  5. use Data::Dumper;
  6.  
  7. my @failxml = (
  8. "<tests>\n",
  9. "<test>\n",
  10. "<id>1</id>\n",
  11. "</test>\n",
  12. "<test>\n",
  13. "<id>2</id>\n",
  14. "</test>\n",
  15. "</tests>\n"
  16. );
  17.  
  18. my @passxml = (
  19. "<tests>\n",
  20. "<test>\n",
  21. "<id>1</id>\n",
  22. "</test>\n",
  23. "<test>\n",
  24. "<id>2</id>\n",
  25. "</test>\n",
  26. "<test>\n",
  27. "</test>\n",
  28. "</tests>\n"
  29. );
  30.  
  31. open (OUT, ">test.xml");
  32. print OUT @failxml;
  33. close (OUT);
  34.  
  35. my $testlist = XMLin('test.xml');
  36.  
  37. print Dumper($testlist);
  38.  
  39. foreach my $test (@{$testlist->{test}}) {
  40.         print $test->{id} ;
  41.     print " ..worked..\n";
  42. }
Change "fail" to "pass" at that "print OUT" statement to see it work, or change id to id1

thanks for looking at my newbie junk code.


====results follow====

C:\perl>test.pl
$VAR1 = {
'test' => {
'1' => {},
'2' => {}
}
};
Not an ARRAY reference at C:\perl\test.pl line 39.
====i changed fail to pass and repeated the test====
C:\perl>test.pl
$VAR1 = {
'test' => [
{
'id' => '1'
},
{
'id' => '2'
},
{}
]
};
1 ..worked..
2 ..worked..
Use of uninitialized value in print at C:\perl\test.pl line 40.
..worked..

C:\perl>
May 30 '10 #1
Share this Question
Share on Google+
2 Replies


Expert
P: 70
Try:

Expand|Select|Wrap|Line Numbers
  1. my $testlist = XMLin('test.xml', ForceArray => 1); 
I recommend switching from XML::Simple to XML::Twig in order to avoid such mysterious behavior.
May 30 '10 #2

BeemerBiker
P: 87
Thanks - that did it. However, that forcearray also stopped hash's from working :-(
May 30 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.