Problem in parsing XML file with empty element


A

anthony

Hello.
I have written some perl code that works fine for a particular XML file
that is parses and converts to CSV format.
my $struct = $simple->XMLin($xmlcontent, forcearray => 1, keeproot =>
1, noattr => 1 );
the above line is what I use to read the elements and extract the data
attached to them.

It works fine when the elements all contain data. However, now I have a
new XML file that I need to parse and one of the elements in most cases
does not contain any data. <element></element>

In my csv output, if the element is empty I get the entry
"HASH(0x20b32200)" appearing.
Can anyone help me on how I can ignore the elements that are empty and
not write anything to the output file?

Thanks
 
Ad

Advertisements

E

Eden Cardim

anthony escreveu:
Hello.
I have written some perl code that works fine for a particular XML file
that is parses and converts to CSV format.
my $struct = $simple->XMLin($xmlcontent, forcearray => 1, keeproot =>
1, noattr => 1 );
the above line is what I use to read the elements and extract the data
attached to them.

It works fine when the elements all contain data. However, now I have a
new XML file that I need to parse and one of the elements in most cases
does not contain any data. <element></element>

In my csv output, if the element is empty I get the entry
"HASH(0x20b32200)" appearing.
Can anyone help me on how I can ignore the elements that are empty and
not write anything to the output file?

Thanks

Well, you should provide some source code to get better replies, but
since you said you're getting references when elements are empty,
simply test the data for "refness", as in:

print $xmldata unless ref $xmldata;

this way, the data wont print if it's a reference to something.
 
E

ekkehard.horner

anthony said:
Hello.
I have written some perl code that works fine for a particular XML file
that is parses and converts to CSV format.
my $struct = $simple->XMLin($xmlcontent, forcearray => 1, keeproot =>
1, noattr => 1 );
the above line is what I use to read the elements and extract the data
attached to them.

It works fine when the elements all contain data. However, now I have a
new XML file that I need to parse and one of the elements in most cases
does not contain any data. <element></element>

In my csv output, if the element is empty I get the entry
"HASH(0x20b32200)" appearing.
Can anyone help me on how I can ignore the elements that are empty and
not write anything to the output file?

Thanks
Read about (and use) the option "SuppressEmpty" in XML::Simple's docs.
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top