4 ms·
Is there a hard and fast rule for deciding when something is an attribute and when it is a child? I would have personally done <user id="123456" name="Joe
by Igglyboo 12y ago
Is there a hard and fast rule for deciding when something is an attribute and when it is a child?
I would have personally done
<user id="123456" name="Joe">
<account>Account 1</account>
<account>Account 2</account>
</user>
but I've never actually produced XML, only consumed.
- seanalltogether 12y agoBoth ways still leave the structure ambiguous. Should <user> be mapped to an associative array of mixed objects, or to a fixed object using a custom mapper to shove the account objects into their own sub array.
- treve 12y agoIf you're looking for a generalized way to map an XML structure to primitive types, you're going to have a bad time.
- pjungwir 12y agoMy rule is that things are almost never attributes. :-) There is less flexibility there. Escaping seems harder, and you can't have sub-elements or lists. Avoiding attributes makes things more verbose, but then that's XML for you.
- fizx 12y agoI've followed the rule that if the property is of the underlying data representation, use children, and if it's a (meta-ish) property of the XML implementation, or some contextual annotation, use an attribute. In your example, something like: <users total="100" cursor="123453"> <user> <id type="int">123456</id> <name type="string">Joe</name> <accounts> <account> <name type="string">Account 1</name> </account> </accounts> </user> </users>