From: "NAKAMURA, Hiroshi" Date: 2004-08-19T14:38:34+09:00 Subject: Re: YAML -> XML Hi, On Thu, 19 Aug 2004 12:36:51 +0900, Nicholas Van Weerdenburg wrote: > Are there any libraries that handle YAML->XML conversion? I want to use > in-line YAML to allow command-line insertion of XML fragments into a > document. > > e.g. addxml.rb --entry "config : {name : url, value : www.blah.com }" > > would enter: > > url > www.blah.com > Just a proof-of-concept thing (not insertion but dumping.) % cat xmlgenerator.rb require 'soap/marshal' class XMLGenerator < SOAP::SOAPGenerator def obj_to_xml(obj) soap = SOAP::Mapping.obj2soap(obj) soap.elename = XSD::QName.new(nil, soap.type.name) convert_to_xml(soap) end def hash_to_xml(hash, name) soap = SOAP::SOAPElement.from_obj(hash) soap.elename = XSD::QName.new(nil, name) convert_to_xml(soap) end private def convert_to_xml(soap) @generate_explicit_type = false ns = XSD::NS.new @buf = '' @indent = '' encode_data(ns, true, soap, nil) @buf end end generator = XMLGenerator.new class Config def initialize(name, value) @name = name @value = value end end obj1 = Config.new("url", "www.blah.com") puts generator.obj_to_xml(obj1) require 'yaml' obj2 = YAML.load("config : {name : url, value : www.blah.com }") puts generator.hash_to_xml(obj2, 'root') % ruby xmlgenerator.rb url www.blah.com url www.blah.com Regards, // NaHi