Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML - parse 1

Status
Not open for further replies.

vietboy505

Technical User
Feb 20, 2006
56
US
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.
 
I try to follow the example from http://search.cpan.org/~grantm/XML-Simple-2.14/lib/XML/Simple.pm"]XML Parse[/URL] with foo.

Code:
#!/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
Code:
  <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, it doesn't match the one with from the web.
 
are you asking a question about why your perl script does not work? if so - better to ask in the perl forum, i'd guess.

i can't help feeling that you're using element attributes as data values too. i don't think that's great xml strategy. would not the following be more normal?:
Code:
    <server>
      <server_name>
        sahara
      </server_name>
      <osname>
        solaris
      </osname>
      <osversion>
        2.6
      </osversion>
      <addresses>
        <address>
         10.0.0.101
        </address>
        <address>
         10.0.1.101
        </address>
      <addresses>
    </server>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top