• Publicidad

XML::Simple / Excel::Writer::XLSX

¿Apenas comienzas con Perl? En este foro podrás encontrar y hacer preguntas básicas de Perl con respuestas aptas a tu nivel.

XML::Simple / Excel::Writer::XLSX

Notapor seguridadenmimail » 2012-08-26 22:13 @967

Estimados,
Tengo un parser XML el cual debo entregarle como argumento un directorio y este se encarga de parsear todos los XML que se encuentren en él.

El problema que tengo es que me parsea todo y me lo guarda en la columna "A" porque yo puse que sea así, pero me gustaría en verdad que las URL sean guardadas en la columna correspondiente a su dominio/hosts, ya que tengo muchos XML que parsear.

Ejemplo de un XML:
Sintáxis: [ Descargar ] [ Ocultar ]
Using xml Syntax Highlighting
  1. <?xml version="1.0"?>
  2. <ScanFULL>
  3.   <Scan>
  4.     <Name><![CDATA[b ( 10.10.10.15 )]]></Name>
  5.     <INITURL><![CDATA[10.10.10.15]]></INITURL>
  6.     <WEBS StartUrl="http://10.10.10.15/">
  7.       <TEST2>
  8.         <TEST3 id="1">
  9.           <Name></Name>
  10.           <TODASLASURL>http://10.10.10.15/</TODASLASURL>
  11.         </TEST3>
  12.         <TEST3 id="2">
  13.           <Name>index.html</Name>
  14.           <TODASLASURL>http://10.10.10.15/index.html</TODASLASURL>
  15.           <INFO>
  16.             <INFORMA>
  17.               <TEXTO>hola</TEXTO>
  18.             </INFORMA>
  19.             <INFORMA>
  20.               <TEXTO>hola2</TEXTO>
  21.             </INFORMA>
  22.             <INFORMA>
  23.               <TEXTO>hola3</TEXTO>
  24.             </INFORMA>
  25.           </INFO>
  26.         </TEST3>
  27.       </TEST2>
  28.     </WEBS>
  29.   </Scan>
  30. </ScanFULL>
  31.  
Coloreado en 0.001 segundos, usando GeSHi 1.0.8.4


Script:

Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. #!/usr/bin/perl
  2.  
  3. my $dir = $ARGV[0];
  4. chomp $dir;
  5.  
  6. use XML::Simple;
  7. my $xml = new XML::Simple( KeyAttr => [] );
  8.  
  9. use Excel::Writer::XLSX;
  10. my $workbook  = Excel::Writer::XLSX->new("salida.xlsx");
  11. my $worksheet = $workbook->add_worksheet();
  12.  
  13. $a = 0;
  14. $b = 0;
  15.  
  16. ## Dirs
  17. @files = <$dir/*.xml>;
  18.  
  19. ## Excel
  20. foreach $file (@files) {
  21.     my $data = $xml->XMLin("$file");
  22.     $worksheet->write( 0, 0 + $a, $data->{Scan}->{INITURL} );
  23.     $a++;
  24.  
  25.     for my $info2 ( @{ $data->{Scan}->{WEBS}->{TEST2}->{TEST3} } ) {
  26.         $worksheet->write( 1 + $b, 0, $info2->{TODASLASURL} );
  27.         $b++;
  28.     }
  29. }
  30.  
  31.  
Coloreado en 0.002 segundos, usando GeSHi 1.0.8.4


¿Alguien me puede dar una mano?

Gracias.
Última edición por explorer el 2012-09-13 14:51 @660, editado 8 veces en total
Razón: Poner </TEXTO>
seguridadenmimail
Perlero nuevo
Perlero nuevo
 
Mensajes: 69
Registrado: 2011-08-30 19:28 @853

Publicidad

Re: XML::Simple / Excel::Writer::XLSX

Notapor explorer » 2012-08-27 04:32 @230

No me queda claro la disposición que quieres tener.

Veo que por cada INITURL pueden haber varios TEST3.

Entonces... La idea es colocar en la primera columna los URL, y en las columnas siguientes los TEST3 encontrados, ¿no?
JF^D Perl programming & Raku programming. Grupo en Telegram: https://t.me/Perl_ES
Avatar de Usuario
explorer
Administrador
Administrador
 
Mensajes: 14480
Registrado: 2005-07-24 18:12 @800
Ubicación: Valladolid, España

Re: XML::Simple / Excel::Writer::XLSX

Notapor seguridadenmimail » 2012-08-27 07:39 @360

La idea es ingresar como nombre de columna los "INITURL".

En mi caso me queda:
Columna "A"=10.10.10.15
Columna "B"=10.10.10.16

porque tengo dos XML, uno para cada IP.

Luego, debajo de la columna "A", me quedan todas las URL del valor "TODASLASURL", ya sean de la 10.10.10.15 y 10.10.10.16.

En verdad yo necesito que me queden las del 10.10.10.15 en la de la "A" y las de la 10.10.10.16 en la de la "B" y así con las demás IP en el caso de tener más XML.
seguridadenmimail
Perlero nuevo
Perlero nuevo
 
Mensajes: 69
Registrado: 2011-08-30 19:28 @853

Re: XML::Simple / Excel::Writer::XLSX

Notapor explorer » 2012-08-27 11:45 @531

Yo haría lo siguiente: ir almacenando la información en un hash de array, por todos los archivos xml, y una vez leídos todos, grabar el resultado en la hoja de cálculo.

Las claves del hash serán las INITURL, mientras que los valores son array que almacenan todos los TEST3 correspondientes a ese INITURL. De esa manera, si llega algún archivo xml con un INITURL ya visto, sus TEST3 se van agregando al final de los TEST3 anteriores.

Algo así:
Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. #!/usr/bin/env perl
  2. use v5.14;
  3. use XML::Simple;
  4.  
  5. my $xml_simple = new XML::Simple( KeyAttr => [] );
  6. my @files = <code_32148*.xml>;
  7. my %url;
  8.  
  9. foreach my $file (@files) {
  10.     my $xml = $xml_simple->XMLin($file);
  11.     my $initurl = $xml->{Scan}->{INITURL};
  12.  
  13.     for my $test3 ( @{ $xml->{Scan}->{WEBS}->{TEST2}->{TEST3} } ) {
  14.         push @{$url{$initurl}}, $test3->{TODASLASURL};
  15.     }
  16. }
  17.  
  18. use Data::Dumper::Simple;
  19. say Dumper %url;
  20.  
Coloreado en 0.001 segundos, usando GeSHi 1.0.8.4
Sintáxis: [ Descargar ] [ Ocultar ]
Using text Syntax Highlighting
%url = (
         '10.10.10.16' => [
                            'http://10.10.10.16/',
                            'http://10.10.10.16/index.html'
                          ],
         '10.10.10.15' => [
                            'http://10.10.10.15/',
                            'http://10.10.10.15/index.html'
                          ],
         '10.10.10.17' => [
                            'http://10.10.10.17/',
                            'http://10.10.10.17/index.html',
                            'http://10.10.10.17/index.html'
                          ]
       );
Coloreado en 0.000 segundos, usando GeSHi 1.0.8.4

Ya solo quedaría volcar la información a la hoja de cálculo, recorriendo la información de %url.
JF^D Perl programming & Raku programming. Grupo en Telegram: https://t.me/Perl_ES
Avatar de Usuario
explorer
Administrador
Administrador
 
Mensajes: 14480
Registrado: 2005-07-24 18:12 @800
Ubicación: Valladolid, España

Re: XML::Simple / Excel::Writer::XLSX

Notapor seguridadenmimail » 2012-08-27 13:10 @590

Perdón, pero ¿cómo podría adaptarlo para escribir esa salida en el Excel?

Saludos.
seguridadenmimail
Perlero nuevo
Perlero nuevo
 
Mensajes: 69
Registrado: 2011-08-30 19:28 @853

Re: XML::Simple / Excel::Writer::XLSX

Notapor explorer » 2012-08-27 17:40 @777

Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. use Excel::Writer::XLSX;
  2. my $workbook  = Excel::Writer::XLSX->new("salida.xlsx");
  3. my $worksheet = $workbook->add_worksheet();
  4.  
  5. my $columna = 0;
  6.  
  7. for my $URL (sort keys %url) {
  8.     $worksheet->write( 0, $columna, $URL );
  9.  
  10.     my $fila = 1;
  11.  
  12.     for my $test3 ( @{ $url{$URL} } ) {
  13.         $worksheet->write( $fila++, $columna, $test3 );
  14.     }
  15.  
  16.     $columna++;
  17. }
Coloreado en 0.001 segundos, usando GeSHi 1.0.8.4
JF^D Perl programming & Raku programming. Grupo en Telegram: https://t.me/Perl_ES
Avatar de Usuario
explorer
Administrador
Administrador
 
Mensajes: 14480
Registrado: 2005-07-24 18:12 @800
Ubicación: Valladolid, España

Re: XML::Simple / Excel::Writer::XLSX

Notapor seguridadenmimail » 2012-08-27 18:25 @809

¡¡¡Muchas gracias!!!
seguridadenmimail
Perlero nuevo
Perlero nuevo
 
Mensajes: 69
Registrado: 2011-08-30 19:28 @853

Re: XML::Simple / Excel::Writer::XLSX

Notapor seguridadenmimail » 2012-09-12 14:44 @655

Estoy observando que cuando tengo un ítem sin información el siguiente fragmento del script me está ingresando el mismo dato para todos, aunque tenga información real.

Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. $DInformation = $info->{Detalles};
  2. if (defined $DInformation and length $DInformation) {
  3.                 $marcos = "a";
  4.                 $worksheet->write( 1+$a, 0, $marcos, $files );
  5.                 $a++;
  6.                 }
  7.                 else {
  8.                 $worksheet->write( 1+$a, 0, $DInformation, $files );
  9.                 $a++;
  10.                 }
  11.  
Coloreado en 0.001 segundos, usando GeSHi 1.0.8.4


Si lo armo así:

Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. $DInformation = $info->{Detalles};
  2. if (defined $DInformation and length $DInformation == 0) {
  3.                 $marcos = "a";
  4.                 $worksheet->write( 1+$a, 0, $marcos, $files );
  5.                 $a++;
  6.                 }
  7.                 else {
  8.                 $worksheet->write( 1+$a, 0, $DInformation, $files );
  9.                 $a++;
  10.                 }
  11.  
Coloreado en 0.001 segundos, usando GeSHi 1.0.8.4


Me pone HASH(0x....) pero sí me agrega el dato verdadero en la fila que lo tiene.
seguridadenmimail
Perlero nuevo
Perlero nuevo
 
Mensajes: 69
Registrado: 2011-08-30 19:28 @853

Re: XML::Simple / Excel::Writer::XLSX

Notapor explorer » 2012-09-12 16:49 @742

Pero la línea

if (defined $DInformation and length $DInformation == 0) {

lo que hace es preguntar si $DInformation está definido, y
si la longitud de $DInformation es 0.

Eso quiere decir que la única opción para que el if() se active es si $DInformation es igual a la cadena vacía ('').

La línea

if (defined $DInformation and length $DInformation) {

la puedes resumir en

if ($DInformation) {

pero... el problema vendría si $DInformation valiese '0' o 0.
JF^D Perl programming & Raku programming. Grupo en Telegram: https://t.me/Perl_ES
Avatar de Usuario
explorer
Administrador
Administrador
 
Mensajes: 14480
Registrado: 2005-07-24 18:12 @800
Ubicación: Valladolid, España

Re: XML::Simple / Excel::Writer::XLSX

Notapor seguridadenmimail » 2012-09-12 17:00 @750

Mira, ahora lo deje así:
Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. $DInformation = $info->{Detalles};
  2. if ($DInformation) {
  3.   $marcos = "a";
  4.   $worksheet->write( 1+$a, 0, $marcos, $files );
  5.   $a++;
  6.   }
  7. else {
  8.   $marcos ="b";
  9.   $worksheet->write( 1+$a, 0, $marcos, $files );
  10.   $a++;
  11.   }
  12.  
Coloreado en 0.001 segundos, usando GeSHi 1.0.8.4


Y en todas las filas me deja como dato "a", siendo que en algunos me debe dejar "b" ya que no todos están sin información.
seguridadenmimail
Perlero nuevo
Perlero nuevo
 
Mensajes: 69
Registrado: 2011-08-30 19:28 @853

Siguiente

Volver a Básico

¿Quién está conectado?

Usuarios navegando por este Foro: Google [Bot] y 12 invitados

cron