in reply to Re: XML :: Simple
in thread XML :: Simple

All the perl code underneath has question marks in places where there needs to be a variable, not an exact word like "type" as a reference, a variable reference. So no word that is in my xml should be in the perl scirpt
<?xml version="1.0" encoding="ISO-8859-1"?> <A> <B> <C> <element type="k" name="p" online="yes"/> <element type="i" name="e" online="yes"/> <element type="y" name="z" online="yes"/> <element type="a" name="q" online="yes"/> <element type="z" name="d" online="yes"/> <element type="t" name="p" online="yes"/> </C> </B> </A>
And after posting here, and reading some stuff I got to somethings like this. This code needs to be universal, I just don't get it with the referencing thing, if it is necassary. I suppose so. But let me just remark that I need to get down to the attributes, only A B and C will be there as nested elements.
#!/usr/bin/perl -- use strict; use warnings; use Cwd; use XML::Simple; my $dir = cwd(); opendir(DIR, "."); my @files = grep(/\.xml$/,readdir(DIR)); closedir(DIR); foreach my $file (@files) { my $xs1 = XML::Simple->new(); my $doc = $xs1->XMLin($file, keyattr=>['????????'], ForceContent=>1, F +orceArray=>1); use Data::Dumper; local $Data::Dumper::Indent=1; for my $sub ( sort keys %{ $doc->{???????} } ){ $rowCount = $tellerR; $worksheet->Cells($rowCount, $colCount)->{Value} = $sub; for my $elem ( sort keys %{ $doc->{??????}{$sub} } ){ # More simalar onces ?? Or... for my $element ( @{ $doc->{???????}{$sub}{$elem} } ){ } } } }

Replies are listed 'Best First'.
Re^3: XML :: Simple
by runrig (Abbot) on Nov 05, 2008 at 22:30 UTC
    my $doc = $xs1->XMLin($file, keyattr=>[], ForceContent=>1, ForceArray= +>1); for my $attribs ( @{$doc->{B}[0]{C}[0]{element}} ) { print "$_: $attribs->{$_}\n" for keys %$attribs; }
      And how do I reach A B C and element without mentioning them in my script eg a loop ?

      Thanks for input!
      Ok, that was a stupid question of mine
      I am not able to reach in to my xml without using any specific xml tage titles.
      This is what I got now:
      <A> <B> <C> <element type="k" name="p" online="yes"/> <element type="i" name="e" online="yes"/> <element type="y" name="z" online="yes"/> <element type="a" name="q" online="yes"/> <element type="z" name="d" online="yes"/> <element type="t" name="p" online="yes"/> </C> </B> </A>
      And
      #!/usr/bin/perl -- use strict; use warnings; use Cwd; use Win32::OLE; use XML::Simple; my $dir = cwd(); opendir(DIR, "."); my @file = grep(/\.xml$/,readdir(DIR)); closedir(DIR); use Data::Dumper; local $Data::Dumper::Indent=1; #print Dumper($doc); my $xs1 = XML::Simple->new(); my $doc = $xs1->XMLin($file[0], keyattr=>[], ForceContent=>1, ForceArr +ay=>1); for my $sub1 ( sort keys %{ $doc } ){ my $count = scalar keys %{ $doc }; print "\n$sub1\t"; for (my $i=0; $i <= $count; $i++) { for my $sub2 ( sort keys %{ $doc->{$sub1}[$i] } ){ my $count2 = @{$doc->{$sub1}[$i]{$sub2}}; print "\n\t\t".$count2."\t".$sub2; for (my $j=0; $j <= $count2; $j++) { for my $sub3 ( sort keys %{ $doc->{$sub1}[$i]{$sub2}[$ +j] } ){ my $count3 = @{$doc->{$sub1}[$i]{$sub2}[$j]{$sub3} +}; print "\n\t\t\t\t$count3\t$sub3"; for (my $k=0; $k <= $count3; $k++) { for my $sub4 ( sort keys %{ $doc->{$sub1}[$i]{ +$sub2}[$j]{$sub3}[$k] } ){ print "\n\t\t\t\t\t$sub4"; } } } } } } }
      This works very nice, I am quite pleased. BUT how do I make a difference between an attribute and a normal tag?
      As you can see in the output ... element (tag) and name type and online (attributes) have no difference, as I see it.
      Every piece of code must NOT contain any words that are in my xml. The code I posted does not contain any 'A' 'B' nor 'C' or 'element'
      -So how can I seperate the tag names from the attributes?
      -How can I reach the content of that attribute ? X in type="X"
      Thanks !

      PS: This script will serve as an XML to Excell converter, I have the Excell part figured out with the OLE32 module. I will post the complete script when its done.

        With XML::Simple you can't tell the difference between an attribute and a sub-tagname. You may want to use some other module, like XML::LibXML.