Basically questions says it all, how can I convert an xml file to yaml?
I’ve tried this:
require 'active_support/core_ext/hash/conversions'
require 'yaml'
file = File.open("data/mconvert.xml", "r")
hash = Hash.from_xml(file.read)
yaml = hash.to_yaml
File.open("data/mirador.yml", "w") { |file| file.write(yaml) }
But, I am getting an “Exception parsing” error. I thought that was because I had dashes in an xml tag name, so I replaced the dashes with dashcharacterr But that still didn’t work.
If we have a look at the XML 1.0 specification, we’ll see that start tags look like this:
and then elsewhere, we find the definition of
Name:You’ll notice that
-is not inNameStartCharso this:is not valid XML and this part of your code:
is failing because your file doesn’t contain XML, it contains text that looks like XML but isn’t quite real XML.
Fix your
data/mconvert.xmlfile to contain real XML and try again.If you try a simple experiment in the Rails console, you’ll see what’s going on:
notice the “malformed XML: missing tag start”?