Problem in parsing XML file with empty element

Discussion in 'Perl Misc' started by anthony, Nov 16, 2005.

  1. anthony

    anthony Guest

    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
     
    anthony, Nov 16, 2005
    #1
    1. Advertising

  2. anthony

    Eden Cardim Guest

    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.
     
    Eden Cardim, Nov 16, 2005
    #2
    1. Advertising

  3. anthony wrote:
    > 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.
     
    ekkehard.horner, Nov 16, 2005
    #3
  4. anthony

    anthony Guest

    I thank you for your responses. SuppressEmpty did it.
    Cheers
     
    anthony, Nov 16, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lukas
    Replies:
    3
    Views:
    837
    spiff
    Nov 10, 2005
  2. jkflens
    Replies:
    2
    Views:
    1,515
    jkflens
    May 30, 2006
  3. HANM
    Replies:
    2
    Views:
    736
    Joseph Kesselman
    Jan 29, 2008
  4. PL
    Replies:
    2
    Views:
    235
    Brian McCauley
    Dec 14, 2004
  5. Erik Wasser
    Replies:
    5
    Views:
    485
    Peter J. Holzer
    Mar 5, 2006
Loading...

Share This Page