Hi All (and a happy holiday to those that will get a break),

I am trying to read in an XML file of addresses. I need to remove all 
the address data from the file where code =~ /^000/ (there are none 
in the example data below). I need to reproduce that data 'as is', so 
I need to honour the tag structure, although the order of the tags 
doesn't need to be honoured.

I have been trying to use XML::Simple and I had a go with XML::Smart 
but I haven't been able to get the results I want.

My best effort is below (with XML::Simple). There are a couple of 
differences I can't seem to get around. 1) How to get a name on the 
anon tags (<anon> should be <address>) . 2) how to get the number 
data as an attribute or a tag.

My script effort is below also. If anyone know of any options with 
XML::Simple that will help please let me know. Failing that, any 
other suggestions on how to tackle the problem would be appreciated.

TIA,
Dp.

======== my script ================
#!/usr/bin/perl

use strict;
use warnings;
use XML::Simple;
use Data::Dumper;

my $fh = shift;
my $out = 'Addresses-no-phgr.xml';
my $xml = eval {XMLin($fh, SuppressEmpty => 1)};

my @addresses;

my $c = @{$xml->{'address'}};
for (my $i = 0; $i < $c; ++$i) {
        my %hash;
        my %lines;
        print $xml->{'address'}->[$i]->{'code'}."\n";
        my @add_lines;
        if ( $xml->{'address'}[$i]->{'lines'} ) {
                my $d = @{$xml->{'address'}[$i]->{'lines'}-
>{'line'}};
                for (my $l = 0; $l < $d; ++$l ) {
                        push(@add_lines, $xml->{'address'}[$i]-
>{'lines'}->{'line'}[$l] );
                }


        }

        
@hash{'code','record_type','address_type','Postcode','Country','number
','lines'} =
                ($xml->{'address'}[$i]->{'code'},$xml-
>{'address'}[$i]->{'record_type'},
                 $xml->{'address'}[$i]->{'address_type'},$xml-
>{'address'}[$i]->{'Postcode'},
                 $xml->{'address'}[$i]->{'Country'},$xml-
>{'address'}[$i]->{'number'},
                 [EMAIL PROTECTED]);

        push @addresses,\%hash;
}


open my $ofh, '>:encoding(iso-8859-1)', $out  or die "Can't write to 
$out: $!\n!";
XMLout([EMAIL PROTECTED],  XMLDecl => 1,
                RootName => 'Addresses',
                ValueAttr => {'lines' => 'line'},
                NoAttr => 1,
                Outputfile => $ofh,
        );

=======================

============= my best effort ==========
<?xml version='1.0' standalone='yes'?>
<Addresses>
  <anon>
    <Country>GBR</Country>
    <Postcode>EC1Y 4RX</Postcode>
    <address_type>shipping</address_type>
    <code>BJPPHO00</code>
    <lines>
      <anon>INCISIVE MEDIA</anon>
      <anon>HAYMARKET HOUSE</anon>
      <anon>28-29 HAYMARKET</anon>
      <anon>LONDON</anon>
      <anon>EC1Y 4RX</anon>
    </lines>
    <number>2577</number>
    <record_type>client</record_type>
  </anon>
  <anon>
    <Country>GBR</Country>
    <Postcode>SU1V 0AQ</Postcode>
    <address_type>shipping</address_type>
    <code>BJPUBL00</code>
    <lines>
      <anon>RIVER HOUSE</anon>
      <anon>12-14 BERRY STREET</anon>
      <anon>SURRY</anon>
      <anon>SU1V 0AQ</anon>
    </lines>
    <number>2578</number>
    <record_type>client</record_type>
  </anon>
</Addresses>
========================

=========== The data file ===========
<?xml version = "1.0" encoding= "utf-8"?>
<addresses>
        <address number="2577">
                <code>BJPPHO00</code>
                <record_type>client</record_type>
                <address_type>shipping</address_type>
                <Postcode>EC1Y 4RX</Postcode>
                <Country>GBR</Country>
                <lines>
                        <line>PIGEON MEDIA</line>
                        <line>HAYMARKET HOUSE</line>
                        <line>28-29 HAYMARKET</line>
                        <line>LONDON</line>
                        <lines>EC1Y 4RX</line>
                </lines>
        </address>
        <address number="2578">
                <code>BJPUBL00</code>
                <record_type>client</record_type>
                <address_type>shipping</address_type>
                <Postcode>SU1V 0AQ</Postcode>
                <Country>GBR</Country>
                <lines>
                        <line>RIVER HOUSE</line>
                        <line>12-14 BERRY STREET</line>
                        <line>SURREY</line>
                        <line>SU1V 0AQ</line>
                </lines>
        </address>
</addresses>
=============================

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


Reply via email to