For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > July 2006 > v2: XML: :Simple-handling {changing element name}









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author v2: XML: :Simple-handling {changing element name}
Bw

2006-07-26, 6:57 pm

I'm not sure that I was completely clear on my
description. As I go through the Dumper output, I
have a different name/entry for each occurence of the
{changing element name} element. I'm looking for
recommendations regarding how to handle such
occurrences.

I looked through the docs and have (to see the first
occurrence) tried in the code:

$data->{dataschemas}->{dataschema}->[0]->{attributes}->{attribute}}
and
$data->{dataschemas}->{dataschema}[0]->{attributes}->{attribute}}

Neither worked and I'm not sure from the XML::Simple
documentation what other options I have to identify
the varying element name in the code.

--- BW <bwbperl@yahoo.com> wrote:

> How (assuming it does) does XML::Simple deal with an
> XML element whose value changes throughout the
> course
> of the Dumper output?
>
> XML::Simple code snippet:
> $data->{dataschemas}->{dataschema}{changing element
> name}->{attributes}->{attribute}}
>
> Dumper output snippet:
>
> (format adjusted for brevity):
> {
> 'dataschemas' => {
> 'dataschema' => {
> 'changing element name' => {
> 'attributes' => {
> 'attribute' => { [
> ... attributes here
> ...
> }
> }
> }
> }
> }
> }
>
> If XML::Simple isn't the right module for the task,
> which is a better one? I looked at the 'where to
> from
> here?' section of the XML::Simple doc to answer
> this,
> but none of the modules briefly described there seem
> to be likely choices at this point.
>
> Thanks,
> BW
>
> ________________________________________
__________
> Do You Yahoo!?
> Tired of spam? Yahoo! Mail has the best spam
> protection around
> http://mail.yahoo.com
>
> --
> To unsubscribe, e-mail:
> beginners-unsubscribe@perl.org
> For additional commands, e-mail:
> beginners-help@perl.org
> <http://learn.perl.org/>
> <http://learn.perl.org/first-response>
>
>
>



________________________________________
__________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
DJ Stunks

2006-07-26, 6:57 pm

Bw wrote:
> I'm not sure that I was completely clear on my
> description. As I go through the Dumper output, I
> have a different name/entry for each occurence of the
> {changing element name} element. I'm looking for
> recommendations regarding how to handle such
> occurrences.
>
> I looked through the docs and have (to see the first
> occurrence) tried in the code:
>
> $data->{dataschemas}->{dataschema}->[0]->{attributes}->{attribute}}
> and
> $data->{dataschemas}->{dataschema}[0]->{attributes}->{attribute}}
>
> Neither worked and I'm not sure from the XML::Simple
> documentation what other options I have to identify
> the varying element name in the code.


I'd like to try to help, but I wasn't able to download the .xml and the
..txt you uploaded earlier in this thread.

anyway, it doesn't sound like your problem is with XML::Simple but
learning how to access and manipulate a complex data structure (in this
case returned from XML::Simple). You would do well to read perlreftut
and perldsc in order to understand how to access them.

in your case, the changing key names, will be keys to a hash. if you
don't know what the key names are ahead of time, just use the keys()
function to get a list of them.

HTH,
-jp

Mumia W.

2006-07-26, 9:57 pm

On 07/26/2006 02:44 PM, BW wrote:
> I'm not sure that I was completely clear on my
> description. As I go through the Dumper output, I
> have a different name/entry for each occurence of the
> {changing element name} element. I'm looking for
> recommendations regarding how to handle such
> occurrences.
> [...]


You handle them the way you'd handle any hash reference. You
iterate over the keys:

use strict;
use warnings;

my $xml = new XML::VeryComplex (KeyAttr=>[]);
my $data = $xml->XMLin(join '',<DATA> );


my $variable = $data->{dataschema}{attributes}{attribute};
print "attribute:\n";
foreach my $key (keys %$variable) {
print qq{ $key => "$variable->{$key}"\n};
}


exit;

package XML::VeryComplex;
use base 'XML::Simple';

package main;
__DATA__
<dataschemas>
<dataschema name="defaultDB">
<includes>
<include name="Base Metadata"/>
<include name="Extracted Re-Map"/>
</includes>
<attributes>
<attribute category="" parser="TextParser"
extract="true" segmentation="soft"/>
</attributes>
</dataschema>
</dataschemas>


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com