How can I convert this XML to YAML and back again without losing information?
<foo bar='one' baz='two'>Lorem Ipsum</foo>
My best guess is something like this, but it looks too verbose to me:
foo:
attr:
- bar: one
- baz: two
value: Lorem Ipsum
Second question: Is there syntax similar to this for attributes in YAML?
foo ( bar: one, baz: two ): Lorem Ipsum
XML is an eXtensible Markup Language while Yaml is a human-readable data-serialization language. Let us look at the comparison of both languages in terms of their file structure, syntax and usage.
The biggest difference, though, is that XML is meant to be a markup language and YAML is really more of a data format. Representing simple, hierarchical data tends to be more gracefully done in YAML, but actual marked-up text is awkward to represent.
YAML is a digestible data serialization language often used to create configuration files with any programming language. Designed for human interaction, YAML is a strict superset of JSON, another data serialization language.
YAML files store information, so they do not include actions and decisions. Unlike XML or JSON, YAML presents data in a way that makes it easy for a human to read. The simple syntax does not impact the language's capabilities. Any data or structure added to an XML or JSON file can also be stored in YAML.
If you can assume you can't have duplicate attribute names, your guess can be simplified to use a map:
foo:
attr:
bar: one
baz: two
value: Lorem Ipsum
If you wanted a barely more compact form, you could use YAML flow style, which is similar to JSON and the closest thing to your second question that is available:
foo: {attr: {bar: one, baz: two}, value: lorum ipsum}
... it isn't very common though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With