Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
bcail opened this issue Jun 19, 2014 · 4 comments
Open

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

bcail opened this issue Jun 19, 2014 · 4 comments
Labels

Comments

@bcail
Copy link

bcail commented Jun 19, 2014

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
Copy link
Author

bcail commented Aug 5, 2014

Ping. Any thoughts on this?

@washcycle
Copy link

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
Copy link
Author

bcail commented Aug 6, 2014

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
Copy link

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

@mvz mvz added the feature label Dec 31, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants