yoreek / XML-Hash-XS

Convert hash to xml
Other
4 stars 4 forks source link

Unexpected result in hash #22

Closed roryzweistra closed 3 years ago

roryzweistra commented 3 years ago

I have unexpected results parsing the attached test.xml

The parsing process seems to miss a closing quote or something. My hash comes out as:

   GuestCharges   {
                            GuestCharge   [
                                [0] {
                                    AddToBaseCharge   "false",
                                    Amount            0,
                                    Board             2,
                                    Duration          "P1D",
                                    End               "2021-12-31",
                                    MaxAge            11,
                                    MaxCount          1,
                                    MinAge            2,
                                    MinCount          1,
                                    PersonType        "C",
                                    Start             "2021-01-03",
                                    Type              "ExtraDay"
                                },
                                [1] {
                                    AddToBaseCharge   "false",
                                    Amount            0,
                                    Board             4,
                                    Duration          "P1D",
                                    End               "2021-12-31",
                                    MaxAge            "11" PersonType="C" />
              <GuestCharge Amount="0" Type="ExtraDay" Board="5" AddToBaseCharge="false" MaxCount="1" MinCount="1" Duration="P1D" Start="2021-01-03" End="2021-12-31" MinAge="2" MaxAge="11" PersonType="C" />
              <GuestCharge Amount="0" Type="ExtraDay" Board="1" AddToBaseCharge="false" MaxCount="1" MinCount="1" Duration="P1D" Start="2021-01-03" End="2021-12-31" MinAge="2" MaxAge="11" PersonType="C" />
            </GuestCharges>
          </ChargeBlock>
        </ChargeBlocks>
        <GlobalTypes>

On the second GuestCharge I expect the MaxAge property to be INT 11 instead of string:

"11" PersonType="C" /> etc.

Using XML::Simple's XMLIN doesn't break on the MaxAge property.

It could be something in the xml file that I have missed but it looks valid to me. Checked for hidden characters too. Any ideas what's going on? I have this problem on both a debian machine and a Mac.

test.txt

Thanks!

Rory

yoreek commented 3 years ago

Sorry but I can't reproduce:

$ perl -e 'use XML::Hash::XS;use Data::Dumper;print Dumper xml2hash("test.txt")'

'GuestCharges' => {
 'GuestCharge' => [
    {
      'PersonType' => 'C',
      'Amount' => '0',
      'MaxAge' => '11',
      'Type' => 'ExtraDay',
      'MinCount' => '1',
      'End' => '2021-12-31',
      'Duration' => 'P1D',
      'MaxCount' => '1',
      'AddToBaseCharge' => 'false',
      'Board' => '2',
      'MinAge' => '2',
      'Start' => '2021-01-03'
    },
    {
      'Type' => 'ExtraDay',
      'MaxAge' => '11',
      'Amount' => '0',
      'PersonType' => 'C',
      'End' => '2021-12-31',
      'MinCount' => '1',
      'AddToBaseCharge' => 'false',
      'MaxCount' => '1',
      'Duration' => 'P1D',
      'Start' => '2021-01-03',
      'MinAge' => '2',
      'Board' => '4'
    },
    {
      'MaxCount' => '1',
      'Duration' => 'P1D',
      'AddToBaseCharge' => 'false',
      'Start' => '2021-01-03',
      'MinAge' => '2',
      'Board' => '5',
      'PersonType' => 'C',
      'Amount' => '0',
      'MaxAge' => '11',
      'Type' => 'ExtraDay',
      'MinCount' => '1',
      'End' => '2021-12-31'
    },
    {
      'MaxAge' => '11',
      'Type' => 'ExtraDay',
      'PersonType' => 'C',
      'Amount' => '0',
      'End' => '2021-12-31',
      'MinCount' => '1',
      'AddToBaseCharge' => 'false',
      'Duration' => 'P1D',
      'MaxCount' => '1',
      'Board' => '1',
      'Start' => '2021-01-03',
      'MinAge' => '2'
    }
  ]
},

Please tell me which version of the module, which operating system and example code of use.

roryzweistra commented 3 years ago

Thanks for your quick reply, I've narrowed it down to passing a filehandle.

This breaks the hash

use XML::Hash::XS;
use Data::Dumper;

open ( my $fh, "<", "test.txt" ) or die "Can't open file 'test.txt'";

my $conv   = XML::Hash::XS->new();
my $hash   = $conv->xml2hash( $fh );

print Dumper $hash;

This works!

use XML::Hash::XS;
use Data::Dumper;

my $conv   = XML::Hash::XS->new();
my $hash   = $conv->xml2hash( "test.txt" );

print Dumper $hash;

I can work around it now, but to my understanding passing a filehandle should work right?

yoreek commented 3 years ago

This bug is reproduced for me and it is related specifically with parsing the file handle I have already fixed it in the new version, please check.

Thanks for report.

roryzweistra commented 3 years ago

Yes updated version works! Thanks for the quick fix.