It's dawned on me that I get YAML and JSON serialization for free now. My default may not work for your default (in fact, it probably won't), but here's what I have:
print $friend->to_xml; print $friend->to_yaml; print $friend->to_json;
That outputs:
--- attributes: name: Victoria some_val: 4 version: 3 elements: - attributes: {} name: personal_email value: victoria@example.com - attributes: {} name: work_email value: victoria@some_company.com { "elements" : [ { "value" : "victoria@example.com", "name" : "personal_email", "attributes" : { } }, { "value" : "victoria@some_company.com", "name" : "work_email", "attributes" : { } } ], "name" : "dummy", "attributes" : { "version" : 3, "name" : "Victoria", "some_val" : 4 } } victoria@example.com victoria@some_company.com
I'll probably have to adjust the code to optionally strip out empty elements and attributes since those look strange in the YAML and JSON.
Update: no, just serializing things directly into JSON and YAML is lousy. This clearly needs work.