How to convert YAML to XML in Perl?

Are there modules in CPAN for converting YAML lists to well-formed XML?

From YAML:

- fruits: - apple - pear - orange 

In XML:

 <fruits> <apple /> <pear /> <orange /> </fruits> 
+6
source share
3 answers
 use strictures; use YAML "Load"; use XML::Simple "XMLout"; my $data = Load(do{ local $/; <DATA> }); print XMLout( $data, XMLDecl => 1 ); __DATA__ --- - fruits: - apple - pear - orange 

There may be a combination of options for XMLout , which will be DWYW, and you may need to compress your data structure to get the root name you need, etc. You can note from a dizzying array of parameters that serialize and sort XML for other data formats is clearly not simple. Read: XML :: Simple and YAML .

+2
source

No, there is no CPAN module for this. there are CPAN modules for reading YAML and there are modules for writing XML . Data transfer will be a perl data structure.

The question is what your XML schema should be, and there are many different XML modules, so there is no perfect answer.

+1
source

CPAN can help you with Util :: XML_YAML_PERL .

 use Util::XML_YAML_Perl; my $obj = Util::XML_YAML_PERL->new(); my $yaml_text = <<UNTIL_HERE; - fruits: - apple - pear - orange UNTIL_HERE print $obj->yaml_to_xml( $yaml_text ); # <fruits> # <apple /> # <pear /> # <orange /> # </fruits> 

Note. This module has a failure in its documentation and can use the patch: Util :: XML_YAML_Perl is the correct package, and not "XML :: YAML_PERL", as in the documentation for it.

0
source

Source: https://habr.com/ru/post/922344/


All Articles