Seth wrote:
> On Apr 4, 6:41 am, [EMAIL PROTECTED] (Rob Dixon) wrote:
>> Seth wrote:
>>> I have an existing xml file that I need to display.  Perl was
>>> suggested a good way to do that.
>>> I am using XML::Simple.  None of the examples use the format that I
>>> have.
>>> Here is an example of what I am dealing with:
>>> <config>
>>> <param name="SequenceNumber">66</param>
>>> <param name="T1">6</param>
>>> <param name="T3">6</param>
>>> <param name="T4">540</param>
>>> <param name="DownloadDate">11-28-07</param>
>>> </config>
>>> I can use Dumper to see that it has been loaded but can't figure out
>>> how to navigate the structure.
>>> Any help would be great.
>> IMO XML::Simple is anything but simple to use, but the program below may
>> help. I can't suggest any more without knowing what sort of output you
>> need.
>>
>> Rob
>>
>> use strict;
>> use warnings;
>>
>> use XML::Simple;
>>
>> my $xml = <<XML;
>> <config>
>> <param name="SequenceNumber">66</param>
>> <param name="T1">6</param>
>> <param name="T3">6</param>
>> <param name="T4">540</param>
>> <param name="DownloadDate">11-28-07</param>
>> </config>
>> XML
>>
>> my $tree = XMLin($xml);
>>
>> use Data::Dumper;
>>
>> foreach my $item (keys %{$tree->{param}}) {
>>   printf "%s => %s\n", $item, $tree->{param}{$item}{content};
>>
>> }
>>
>> **OUTPUT**
>>
>> T4 => 540
>> T1 => 6
>> SequenceNumber => 66
>> DownloadDate => 11-28-07
>> T3 => 6
> 
> Thanks for the response Rob.
> If XML::Simple is hard to use what would be the better way to do this?
> 
> Basically I need to read the config.xml file in the format above and
> display it like a form then take inputs if changed.

If that is the full extent of your config file I would be unwise to
suggest you use anything other than XML::Simple, as the problem is
trivial. I would revise my code by adding an empty KeyAttr list though,
as it will make the rest of the code more concise and also maintain the
order of the XML elements.

my $tree = XMLin($xml, KeyAttr => []);

foreach my $param (@{$tree->{param}}) {
  printf "%s => %s\n", $param->{name}, $param->{content};
}

XML::LibXML is an excellent and complrehensive module, but it relies on
libxml2 being installed and may be overkill in this instance. Code using
that module would look something like

my $parser = XML::LibXML->new;
my $doc = $parser->parse_string($xml);

foreach my $param ($doc->findnodes('/config/param')) {
  printf "%s => %s\n", $param->getAttribute('name'), $param->textContent;
}

XML::Twig is also good, and I am sure Jenda would give you a hand with
XML::Rules if you chose that route, as it is his creation.

HTH,

Rob


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to