XML Parsing: remove hash references of empty hashes

myvach
myvach used Ask the Experts™
on
hello

I use the XML::LibXML::Simple perl module to parse xml documents. now if an element is empty, i get a hash reference back instead of a preferred empty string. How can I avoid this as it writes me the hash reference to the database instead of a ''?

The old XML::Simple module has a parameter "SuppressEmpty" to achieve this, but unfortunately this parameter is not supported by xmllib
The ref containing the obsolete hash reference if the element is empty: $member->{district}->{value}->{string}
 
----------------SAMPLE XML SOURCE WITH AN EMPTY TAG 'DISTRICT'-------------
<value><struct><member><name>district</name><value><string></string></value></member>
<member>...........
------------------------------------------------------------------------------------------------

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
quote from perl doc for XML::LibXML::Simple ...

"Also, empty elements are not removed: being empty has a meaning which should not be ignored"

You wont be able to ignore empty elements with this module.
Commented:
your're right, but how can i avoid to have a hash reference written in my database? i've found a workaround yet:

unless( ref($member->{district}->{value}->{string}) ) { $district = $member->{district}->{value}->{string} };

but maybe there is a smarter way without the need to check each variable if it's a reference?

 

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial