• Publicidad

Ejemplos XML::CSV

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

Ejemplos XML::CSV

Notapor luien » 2006-11-14 13:00 @583

Por favor si alguno tuviera un ejemplo de cómo utilizar esta librería exactamente en este ejemplo:

Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
use XML::CSV;
$csvo = XML::CSV->new();
$csvo->{column_heading} = \@arr_of_headings;
$csvo->parse_doc("A.csv");
$csvo->print_xml("Z.xml", {format => " ", file_tag = "x.xml", parent_tag => "alfa"});
Coloreado en 0.002 segundos, usando GeSHi 1.0.8.4


me sale un error :

Código: Seleccionar todo
Can't modify constant item in scalar assignment at arch.pl line 5, near "x.xml","
luien
Perlero nuevo
Perlero nuevo
 
Mensajes: 8
Registrado: 2006-11-08 17:54 @787

Publicidad

Notapor explorer » 2006-11-14 15:31 @688

El error es porque falta un '>': file_tag => "x.xml",
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

Notapor luien » 2006-11-15 09:27 @435

muchas gracias por la ayuda :D
luien
Perlero nuevo
Perlero nuevo
 
Mensajes: 8
Registrado: 2006-11-08 17:54 @787

Notapor luien » 2006-11-15 09:29 @437

¿Alguien tiene ejemplos de cómo utilizar el XML::CSV? mi pregunta exacta es ¿cómo hago para poner los atributos con esta librería en un tag xml?

Tengo un csv con la siguiente estructura:
Código: Seleccionar todo
area1,valor1,comun

y quiero que salga:
Sintáxis: [ Descargar ] [ Ocultar ]
Using xml Syntax Highlighting
<Property Name="area1" Value="valor1" Vista="comun"/>
Coloreado en 0.000 segundos, usando GeSHi 1.0.8.4

y no así:
Sintáxis: [ Descargar ] [ Ocultar ]
Using xml Syntax Highlighting
<tr0>area1</tr0>
<tr1>valor1</tr1>
<tr2>comun</tr2>
Coloreado en 0.000 segundos, usando GeSHi 1.0.8.4


Por favor si alguien me podría ayudar como hacerlo gracias.
luien
Perlero nuevo
Perlero nuevo
 
Mensajes: 8
Registrado: 2006-11-08 17:54 @787

Notapor explorer » 2006-11-16 04:17 @220

Si tenemos un fichero llamado A.csv:
Código: Seleccionar todo
area1,valor1,comun1
area2,valor2,comun2
area3,valor3,comun3
area4,valor4,comun4
area5,valor5,comun5
area6,valor6,comun6
area7,valor7,comun7
area8,valor8,comun8
area9,valor9,comun9
luego, con el siguiente programa:
Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
#!/usr/bin/perl -w

use XML::Simple;

$i = 0;
open CSV,"<A.csv" or die "ERROR: No pude abrir A.csv: $!\n";
while ($linea = <CSV>) {
    chomp $linea;
    @valores = split(',', $linea);

    $xml{Property}[$i]{Name}  = $valores[0];
    $xml{Property}[$i]{Value} = $valores[1];
    $xml{Property}[$i]{Vista} = $valores[2];

    $i++;
}
close CSV;

print XMLout(\%xml, RootName=>'Properties', xmldecl => '<?xml version="1.0">');
Coloreado en 0.001 segundos, usando GeSHi 1.0.8.4
el resultado es:
Sintáxis: [ Descargar ] [ Ocultar ]
Using xml Syntax Highlighting
<?xml version="1.0">
<Properties>
  <Property Name="area1" Value="valor1" Vista="comun1" />
  <Property Name="area2" Value="valor2" Vista="comun2" />
  <Property Name="area3" Value="valor3" Vista="comun3" />
  <Property Name="area4" Value="valor4" Vista="comun4" />
  <Property Name="area5" Value="valor5" Vista="comun5" />
  <Property Name="area6" Value="valor6" Vista="comun6" />
  <Property Name="area7" Value="valor7" Vista="comun7" />
  <Property Name="area8" Value="valor8" Vista="comun8" />
  <Property Name="area9" Value="valor9" Vista="comun9" />
</Properties>
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

Notapor explorer » 2006-11-16 04:47 @241

Si tenemos un fichero llamado A.csv:
Código: Seleccionar todo
area1,valor1,comun1
area2,valor2,comun2
area3,valor3,comun3
area4,valor4,comun4
con el programa:
Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
#!/usr/bin/perl -w

use XML::CSV;

$csvo = XML::CSV->new();
$csvo->{column_headings} = [ qw(Name Value Vista) ];
$csvo->declare_xml({ version => 1.0, standalone => 'yes' });
$csvo->parse_doc("A.csv");
$csvo->print_xml("Z.xml", { file_tag => "Properties", parent_tag => "Property"});
Coloreado en 0.001 segundos, usando GeSHi 1.0.8.4
genera un fichero Z.xml así:
Sintáxis: [ Descargar ] [ Ocultar ]
Using xml Syntax Highlighting
<?xml version="1" standalone="yes"?>
<Properties>
        <Property>
                <Name>area1</Name>
                <Value>valor1</Value>
                <Vista>comun1</Vista>
        </Property>
        <Property>
                <Name>area2</Name>
                <Value>valor2</Value>
                <Vista>comun2</Vista>
        </Property>
        <Property>
                <Name>area3</Name>
                <Value>valor3</Value>
                <Vista>comun3</Vista>
        </Property>
        <Property>
                <Name>area4</Name>
                <Value>valor4</Value>
                <Vista>comun4</Vista>
        </Property>
</Properties>
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

Notapor explorer » 2006-11-16 05:03 @252

Uniendo los dos programas se obtiene lo que deseas:
Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
#!/usr/bin/perl -w

use XML::CSV;
use XML::Simple;

$csvo = XML::CSV->new();
$csvo->{column_headings} = [ qw(Name Value Vista) ];
$csvo->declare_xml({ version => 1.0, standalone => 'yes' });
$csvo->parse_doc("A.csv");

$filas = $csvo->{column_data};
$i = 0;
foreach $fila ( @$filas ) {

    $xml{Property}[$i]{Name}  = $fila->[0];
    $xml{Property}[$i]{Value} = $fila->[1];
    $xml{Property}[$i]{Vista} = $fila->[2];

    $i++;
}

print XMLout(\%xml, RootName=>'Properties', xmldecl => '<?xml version="1.0">');
Coloreado en 0.001 segundos, usando GeSHi 1.0.8.4
sale:
Sintáxis: [ Descargar ] [ Ocultar ]
Using xml Syntax Highlighting
<?xml version="1.0">
<Properties>
  <Property Name="area1" Value="valor1" Vista="comun1" />
  <Property Name="area2" Value="valor2" Vista="comun2" />
  <Property Name="area3" Value="valor3" Vista="comun3" />
  <Property Name="area4" Value="valor4" Vista="comun4" />
</Properties>
Coloreado en 0.000 segundos, usando GeSHi 1.0.8.4

El procedimiento es leer los datos leídos desde $csvo->{column_data}, que devuelve una referencia a un array donde están todos los datos. Luego hacemos un bucle por todas las @$filas de esos datos. Por cada $fila, guardamos esos datos de la misma manera que en el primer ejemplo.
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

Notapor luien » 2006-11-16 09:29 @437

muchas gracias, algo por ahi estaba intentando pero me diste una gran ayuda. Otra vez gracias. :D
luien
Perlero nuevo
Perlero nuevo
 
Mensajes: 8
Registrado: 2006-11-08 17:54 @787

Notapor creating021 » 2006-11-16 17:37 @775

luien escribiste:¿Alguien tiene ejemplos de cómo utilizar el XML::CSV? mi pregunta exacta es ¿cómo hago para poner los atributos con esta librería en un tag xml?

Tengo un csv con la siguiente estructura:
Código: Seleccionar todo
area1,valor1,comun

y quiero que salga:
Sintáxis: [ Descargar ] [ Ocultar ]
Using xml Syntax Highlighting
<Property Name="area1" Value="valor1" Vista="comun"/>
Coloreado en 0.000 segundos, usando GeSHi 1.0.8.4

y no así:
Sintáxis: [ Descargar ] [ Ocultar ]
Using xml Syntax Highlighting
<tr0>area1</tr0>
<tr1>valor1</tr1>
<tr2>comun</tr2>
Coloreado en 0.000 segundos, usando GeSHi 1.0.8.4


Por favor si alguien me podría ayudar como hacerlo gracias.

Como te dije ya en otro foro, CSV::Template era una buena solución pero biedo Parser::CVS es más ideal junto con lo que pone explorer pero de una forma mas espesifica, solo tenes que mirar en CPAN :wink:
Expect the worst, is it the least you can do?
Avatar de Usuario
creating021
Perlero frecuente
Perlero frecuente
 
Mensajes: 595
Registrado: 2006-02-23 16:17 @720
Ubicación: Frente al monitor

Notapor luien » 2006-11-20 09:24 @433

gracias no pude ver el otro foro pero es otra opcion para hacerlo de nuevo muchas gracias por la ayuda.
luien
Perlero nuevo
Perlero nuevo
 
Mensajes: 8
Registrado: 2006-11-08 17:54 @787


Volver a Básico

¿Quién está conectado?

Usuarios navegando por este Foro: No hay usuarios registrados visitando el Foro y 10 invitados

cron