View Full Version : XML - parse in
vietboy505
03-03-2006, 02:45 AM
I was wondering how I can parse XML using PHP or Perl to output as a nice table & edit later?
If so, how would that work? I am a newbie.
FishMonger
03-03-2006, 03:13 AM
http://search.cpan.org/~grantm/XML-Simple-2.14/lib/XML/Simple.pm
http://search.cpan.org/~mirod/XML-Twig-3.23/Twig.pm
http://search.cpan.org/~mikewong/XML-Dumper-0.79/Dumper.pm
vietboy505
03-04-2006, 06:17 AM
I try to follow the example from XML Parse (http://search.cpan.org/~grantm/XML-Simple-2.14/lib/XML/Simple.pm) with foo.
#!/usr/local/bin/perl
use XML::Simple;
my $ref = XMLin('foo.xml');
#also with my $ref = XMLin();
use Data::Dumper;
print Dumper($config);
print $config->{logdir};
where foo.xml
<config logdir="/var/log/foo/" debugfile="/tmp/foo.debug">
<server name="sahara" osname="solaris" osversion="2.6">
<address>10.0.0.101</address>
<address>10.0.1.101</address>
</server>
<server name="gobi" osname="irix" osversion="6.5">
<address>10.0.0.102</address>
</server>
<server name="kalahari" osname="linux" osversion="2.0.34">
<address>10.0.0.103</address>
<address>10.0.1.103</address>
</server>
</config>
I try to test out foo.pl and get
$VAR1 = undef;
Why is that?
FishMonger
03-04-2006, 07:55 AM
It's undef because you're trying to print $config which you never assigned. You should be printing $ref.
#!/usr/local/bin/perl
use XML::Simple;
use Data::Dumper;
my $ref = XMLin('foo.xml');
print Dumper($ref);
#print $ref->{logdir};
Outputs:
$VAR1 = {
'debugfile' => '/tmp/foo.debug',
'server' => {
'kalahari' => {
'osversion' => '2.0.34',
'osname' => 'linux',
'address' => [
'10.0.0.103',
'10.0.1.103'
]
},
'sahara' => {
'osversion' => '2.6',
'osname' => 'solaris',
'address' => [
'10.0.0.101',
'10.0.1.101'
]
},
'gobi' => {
'osversion' => '6.5',
'osname' => 'irix',
'address' => '10.0.0.102'
}
},
'logdir' => '/var/log/foo/'
};
vietboy505
03-05-2006, 05:24 PM
I see that I can get logdir out by using
print $ref->{logdir};
what about this & and it's not working:
print $ref->{server}->{kalahari}->{address}->[1];
print $ref->{server}->{kalahari}->{address};
FishMonger
03-05-2006, 06:03 PM
This one should be working.
print $ref->{server}->{kalahari}->{address}->[1];
The other one won't work because it's an array and you're not specifing which index you want to print.
Try this:print "@{$ref->{server}->{kalahari}->{address}}";
or this:foreach (@{$ref->{server}->{kalahari}->{address}}) {
print "IP:$_\n";
}
vietboy505
03-05-2006, 07:52 PM
thanks FishMonger , that works.
Now I want to make an address book.
There are two types, one for Work other for Relative.
How would I go through all the Work category and grab all those info?
Should I stick the Work category format or the Relative format?
Work have the <personName> right away then their infos.
Where Relative have <person> then names info.
<Address>
<Work>
<JohnDoe>
<name>John Doe</name>
<email>john@doe.com</email>
<phone>555-555-5555</phone>
<pager>6666666666</pager>
<id>123456</id>
</JohnDoe>
<JackDoe>
<name>Jack Doe</name>
<email>jack@doe.com</email>
<phone>666-666-6666</phone>
<pager>7777777777</pager>
<id>05789</id>
</JackDoe>
</Work>
<Relative>
<person>
<name>Jane Doe</name>
<email>jane@doe.com</email>
<phone>444-444-4444</phone>
<pager>8888888888</pager>
<id>654321</id>
</person>
</Relative>
</Address>
Thanks for your help.
FishMonger
03-05-2006, 08:31 PM
foreach my $person (keys %{$ref->{Work}}) {
print $ref->{Work}->{$person}->{name},$/;
print $ref->{Work}->{$person}->{email},$/;
print $ref->{Work}->{$person}->{phone},$/;
print $ref->{Work}->{$person}->{pager},$/;
print $ref->{Work}->{$person}->{id},$/,$/;
}
Or
foreach my $type (qw(Work Relative)) {
foreach my $person (keys %{$ref->{$type}}) {
print $ref->{$type}->{$person}->{name},$/;
print $ref->{$type}->{$person}->{email},$/;
print $ref->{$type}->{$person}->{phone},$/;
print $ref->{$type}->{$person}->{pager},$/;
print $ref->{$type}->{$person}->{id},$/,$/;
}
}
Or
foreach my $type (qw(Work Relative)) {
print "$type group\n";
foreach my $person (keys %{$ref->{$type}}) {
foreach my $field (qw(name email phone pager id)) {
print $ref->{$type}->{$person}->{$field},$/;
}
print $/;
}
}
vietboy505
03-05-2006, 11:56 PM
Awesome.
What if I want to grab the data from a server output to .txt file.
Do I use XMLout() to export the text into a xml?
Where .txt file include:
John Doe
john@doe.com
555-555-5555
6666666666
123456
....
to the same output as:
<Address>
<Work>
<JohnDoe>
<name>John Doe</name>
<email>john@doe.com</email>
<phone>555-555-5555</phone>
<pager>6666666666</pager>
<id>123456</id>
</JohnDoe>
<JackDoe>
<name>Jack Doe</name>
<email>jack@doe.com</email>
<phone>666-666-6666</phone>
<pager>7777777777</pager>
<id>05789</id>
</JackDoe>
</Work>
<Relative>
<JaneDoe>
<name>Jane Doe</name>
<email>jane@doe.com</email>
<phone>444-444-4444</phone>
<pager>8888888888</pager>
<id>654321</id>
</JaneDoe>
</Relative>
</Address>
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.