yoreek / XML-Hash-XS

Convert hash to xml
Other
4 stars 4 forks source link

empty hash keys result in invalid xml #13

Closed sezal closed 5 years ago

sezal commented 6 years ago

Serialization of hash with empty keys produces invalid XML. Consider switching to XML::Simple's approach, because it safer (produces valid XML).

use XML::Simple;
use XML::Hash::XS;

say hash2xml ({foo => 'FOO', '' => 'x'}, indent => 1, xml_decl => 0);
say XMLout ({foo => 'FOO', '' => 'x'}, NoAttr => 1);
<root>
 <>x</>
 <foo>FOO</foo>
</root>

<opt>
x
  <foo>FOO</foo>
</opt>
yoreek commented 6 years ago

In XML::Simple its own logic and it is not always correct in all cases, for example:

$ perl -e "use XML::Simple;print XMLout({'' => 'x'})"
<opt ="x" />

$ perl -e "use XML::Simple;print XMLout({'' => ['x', 'y']}, NoAttr => 1)"
<opt>
  <>x</>
  <>y</>
</opt>

I think it's better to replace empty tags with a line like "_" or something else customizable, for example:

<opt "_"="x" />
<opt>
  <_>x</_>
  <_>y</_>
</opt>
sezal commented 6 years ago

I see. You probably right. Any solution that results in valid XML is already step forward. Btw, attribute name in you example probably should be unquoted.

yoreek commented 5 years ago

outdated