Alternatives to XML?

Rob Gaddi rgaddi at highlandtechnology.invalid
Wed Aug 24 13:12:34 EDT 2016


Frank Millman wrote:

> Hi all
>
> I have mentioned in the past that I use XML for storing certain structures 
> 'off-line', and I got a number of comments urging me to use JSON or YAML 
> instead.
>
> In fact XML has been working very well for me, but I am looking into 
> alternatives simply because of the issue of using '>' and '<' in attributes. 
> I can convert them to '>' and '<', but that imposes a cost in terms of 
> readability.
>
> Here is a simple example -
>
> <case>
>   <compare src="_param.auto_party_id" op="is_not" tgt="$None">
>     <case>
>       <on_insert>
>         <auto_gen args="_param.auto_party_id"/>
>       </on_insert>
>       <not_exists>
>         <literal value="<new>"/>
>       </not_exists>
>     </case>
>   </compare>
> </case>
>
> This is equivalent to the following python code -
>
> if _param.auto_party_id is not None:
>     if on_insert:
>         value = auto_gen(_param.auto_party_id)
>     elif not_exists:
>         value = '<new>'
>
> The benefit of serialising it is partly to store it in a database and read 
> it in at runtime, and partly to allow non-trusted users to edit it without 
> raising security concerns.
>
> I ran my XML through some online converters, but I am not happy with the 
> results.
>
> Here is a JSON version -
>
> {
>   "case": {
>     "compare": {
>       "-src": "_param.auto_party_id",
>       "-op": "is_not",
>       "-tgt": "$None",
>       "case": {
>         "on_insert": {
>           "auto_gen": { "-args": "_param.auto_party_id" }
>         },
>         "not_exists": {
>           "literal": { "-value": "<new>" }
>         }
>       }
>     }
>   }
> }
>
> I can see how it works, but it does not seem as readable to me. It is not so 
> obvious that 'compare' has three arguments which are evaluated, and if true 
> the nested block is executed.
>
> Here is a YAML version -
>
> case:
> compare:
>   case:
>    on_insert:
>     auto_gen:
>      _args: "_param.auto_party_id"
>    not_exists:
>     literal:
>      _value: "<new>"
>   _src: "_param.auto_party_id"
>   _op: is_not
>   _tgt: "$None"
>
> This seems even worse from a readability point of view. The arguments to 
> 'compare' are a long way away from the block to be executed.
>
> Can anyone offer an alternative which is closer to my original intention?
>
> Thanks
>
> Frank Millman
>

You've been staring at that XML too long; you've become familiar with
it.  It's just as unreadable as the JSON or the YAML unless you already
know what it says.

That said, one core difference between XML and the other two is that XML
allows for the idea that order matters.  JSON and YAML both consider
key/value pair objects in the same way Python does a dict -- there is AN
order because there has to be one, but it's not expected to be
important or preserved. And when you start talking about "close to"
you're talking about preserving ordering.

-- 
Rob Gaddi, Highland Technology -- www.highlandtechnology.com
Email address domain is currently out of order.  See above to fix.



More information about the Python-list mailing list