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

how do I reference key in xml::simpleview

BeemerBiker
P: 87
The following work fine:

$doc->{computer}[1];

I wasted about 2 hours but can't figure out how to access the same thing using "foreach"
$rows = \@($doc->{computer}) seemed like it should have worked but it didnt.

Once the foreach works, I should be able to use something like
# foreach my $key (keys (%{\@doc->{computer}}))
# {
# print $doc->{computer}->{$key}->{'id_name'};
# print $doc->{computer}->{$key}->{'ip'};
# }

anyway, the following code can be run from a perl directory but you may want to comment out all the code from line 43 down.
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 (
  8. $row,
  9. $rows,
  10. @thisxml,
  11. $tree,
  12. $doc
  13. );
  14.  
  15. @thisxml = (
  16. "<computers>\n",
  17. "<computer>\n",
  18. "<id_name>linux1</id_name>\n",
  19. "<ip>192.168.0.10</ip>\n",
  20. "</computer>\n",
  21. "<computer>\n",
  22. "<id_name>linux2</id_name>\n",
  23. "<ip>192.168.0.12</ip>\n",
  24. "</computer>\n",
  25. "</computers>\n"
  26. );
  27.  
  28. open (OUT, ">file.xml");
  29. print OUT @thisxml;
  30. close (OUT);
  31.  
  32.  
  33. $tree = XML::Simple->new(KeepRoot=>0, ForceArray=>1, SuppressEmpty=>'',);
  34.  
  35. $doc = $tree->XMLin("file.xml");
  36.  
  37. print Dumper($doc->{computer}[0]);
  38. print "next\n";
  39. print Dumper($doc->{computer}[1]);
  40. print "try foreach\n";
  41.  
  42.  
  43. $rows = \@($doc->{computer});
  44.  
  45. foreach $row($rows) {
  46.     print dumper $row;
  47.     print "next\n";
  48. }
===the following is the output===
C:\perl>test2.pl
$VAR1 = {
'ip' => [
'192.168.0.10'
],
'id_name' => [
'linux1'
]
};
next
$VAR1 = {
'ip' => [
'192.168.0.12'
],
'id_name' => [
'linux2'
]
};
try foreach

C:\perl>
==how to use ForEach and how to use key ?

Thanks for looking!
May 29 '10 #1
Share this Question
Share on Google+
1 Reply


BeemerBiker
P: 87
Found an answer here http://interoperating.info/courses/perl4data/node/26

However, I had tried what should have worked earlier but it failed because, unaccountably, the following line failed to be parsed correctly in my xml file

<id>0</id>

If I substutite "<id1>" for "<id>" and "</id1>" for "</id>" everywhere, then the source xml can be parsed. AFAICT the phrase "<id>" or </id> is not a reserved word.

Perl -v shows the following

v5.10.1 built for MSWin32-x86-multi-thread

I also found that if I simply add a null section to the end of my xml file ie:
<computer>
</computer>

then all the "<id>" are accepted and my programs runs correctly.

This seems to be a bug in perl 5.10.1
May 30 '10 #2

Post your reply

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