Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert YAML to XML in Perl?

Tags:

xml

yaml

perl

Are there any modules in CPAN to convert YAML lists to well formed XML?

From YAML:

- fruits: 
  - apple
  - pear
  - orange

To XML:

<fruits>
  <apple />
  <pear />
  <orange />
</fruits>
like image 381
GeneQ Avatar asked Aug 08 '12 09:08

GeneQ


2 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 might be a combination of options for XMLout that will DWYW and you may need to jigger your data structure to get the root name you want, etc. You'll note from the dizzying array of options that serializing and marshalling XML to other data formats is decidedly not simple. Reading: XML::Simple and YAML.

like image 102
Ashley Avatar answered Nov 09 '22 06:11

Ashley


No there is no CPAN module for that. there are CPAN modules to read YAML and there are modules to write XML. The data transfer will be perl data structure.

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

like image 24
h4ck3rm1k3 Avatar answered Nov 09 '22 05:11

h4ck3rm1k3