dm-serializer generates invalid xml
Reported by Postmodern | May 29th, 2008 @ 07:10 AM
Since to_xml uses DataMapper::Inflector.underscore, which replaces /::/ with '/', to convert class names to xml element names the resulting XML string cannot be parsed by REXML. XML element names should probably never contain '/'.
require 'data_mapper'
require 'dm-serializer'
module Ronin
class Platform
include DataMapper::Resource
property :os, String
property :version, String
end
end
>> xml = Ronin::Platform.new(:os => 'Linux', :version => '2.4.24').to_xml
=> "<ronin/platform id=''><os>Linux</os><version>2.4.24</version></ronin/platform>"
>> REXML::Document.new(xml)
REXML::ParseException: #<REXML::ParseException:
malformed XML: missing tag start
Line:
Position:
Last 80 unconsumed characters:
>> xml.gsub!(/ronin\/platform/,'ronin-platform')
>> REXML::Document.new(xml).root
=> <ronin-platform id=''> ... </>
It should also be noted that to_xml does not add the standard XML declaration to the output.
<?xml version="1.0"?>
Comments and changes to this ticket
-
Jan Kubr June 20th, 2008 @ 04:57 AM
I quickly rewrote the to_xml method to generate XML with declarations, indenting and wrapping tags for collections.
Code:
Spec:
The used xml_element_name is problematic not only for classes wrapped in a module, but also as an instance method it makes its usage in the Collection#to_xml method ugly (@model.new.send(:xml_element_name)
I can create a patch from this if you find it useful (and tell me what needs to be improved).
-
Dan Kubb June 20th, 2008 @ 08:28 AM
Doesn't the current to_xml output stuff like type="integer" or other type attributes to match the ActiveRecord and ActiveResource conventions?
-
Jan Kubr June 20th, 2008 @ 08:42 AM
What is does is it adds keys of the resource as attributes of the tag, so for user with key "id" it generates this opening tag:
I'm not sure why though. And it is different form what ARs do, isn't it.
-
Please Login or create a free account to add a new comment.
You can update this ticket by sending an email to from your email client. (help)
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
