mvz / happymapper

Object to XML mapping library, using Nokogiri (Fork from John Nunemaker's Happymapper)
http://github.com/mvz/happymapper/
MIT License
152 stars 44 forks source link

XML elements that HappyMapper doesn't understand are lost #49

Open bcail opened 10 years ago

bcail commented 10 years ago

class Address include HappyMapper tag 'address' element :street, String end XML = "<address><street>Long St.</street><city>Chicago</city></address>" a = Address.parse(XML) a.to_xml => "<?xml version=\"1.0\"?>\n<address>\n <street>Long St.</street>\n</address>\n"

It'd be nice if the elements that it doesn't understand could still be passed thru to the output. Otherwise, there's the danger of losing data.

bcail commented 9 years ago

Ping. Any thoughts on this?

washcycle commented 9 years ago

I'm not sure I see the use you are looking for. I believe the to_xml shouldn't be changed as that directly converts the defined class into xml based on the mapping. It is not really passing through anything it is converting each element of the ruby class into a new XML output.

Are you hoping to get a robust or automated XML to ruby class mapper that doesn't require the class definition to define the mapping? That might be out of the scope of what happymapper is meant to do. However i'm not one of the developers of it.

One option would be to check for missing xml tags, is to define an XML schema. Then output could be validated to said schema. This would be a unit test type of exercise until the class had all the mapping required by the schema.

In summary and in my humble opinion if that data fragment was needed it should have been defined in the Address class.

bcail commented 9 years ago

Thanks for taking the time to reply.

The goal is to be able to use happymapper without being absolutely sure that every element and attribute is defined in the classes. It's a bit of a safety net against data loss.

Here's what I don't want to happen: I use happymapper to parse & edit some MODS data (or any data), and then save my changes. Then, I realize that I was missing some attributes in the class definition, and now that data is gone. This could even happen because I used MODS v 3.4, and someone creating data used MODS 3.5, which might have more attributes. That data would be lost. So, if I want to use HappyMapper, I need to make sure that all the data coming in doesn't have any new attributes or elements defined. It just doesn't seem safe.

I would be fine with to_xml staying the same as it currently is. What if another method were added for preserving all the data that was passed in?

I've been playing with the idea of trying to track the underlying nokogiri document when the incoming data is first parsed, and then being able to retrieve that doc and call to_xml on it. That's similar to the has_xml_content method that's already present. However, the key is that any changes made to any attributes or elements need to update the underlying data, which doesn't currently happen.

evanrolfe commented 6 years ago

I've been playing with the idea of trying to track the underlying nokogiri document when the incoming data is first parsed, and then being able to retrieve that doc and call to_xml on it.

@bcail I realise this thread is 3 years old but I've created a gem which does exactly what you describe (in order to preserve unspecified content in the original xml file):

https://github.com/evanrolfe/lazy_xml_model