Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON inside XML

I want to know about standard and best practice in :

Case Scenario :

If a xml standard exist by "XYZ SPECIFICATION" (ex : BPMN Specification) like :

<home>
<person name="ZEN" />
<person name="PAUL" />
<animal name="DOG" />
<animal name="CAT" />
</home>

and "XYZ SPECIFICATION" also provide extension element to define your own tag like :

   <home>
    <person name="ZEN" />
    <person name="PAUL" />
    <animal name="DOG" />
    <animal name="CAT" />
    <extension>
    <instrument-list>
    <type name="acoustic">
      <instrument name="GUITAR" />
      <instrument name="VIOLIN"  />
    </type>
    <type name="electronic">
      <instrument name="GUITAR" />
      <instrument name="VIOLIN"/>
    </type>
    </instrument-list>
    </extension>
    </home>

Tag defined by standard are used by other client parser , they don't need to parse extension tag so i was thinking that it would be better to compress extension element using json (because json takes less space in compare to xml) :

  <home>
    <person name="ZEN" />
    <person name="PAUL" />
    <animal name="DOG" />
    <animal name="CAT" />
    <extension>
    <instrument-list>{"type":[{"name":"acoustic","instrument":[{"name":"GUITAR"},{"name":"VIOLIN"}]},{"name":"electronic","instrument":[{"name":"GUITAR"},{"name":"VIOLIN"}]}]}</instrument-list>
    </extension>
    </home>

or

<home>
    <person name="ZEN" />
    <person name="PAUL" />
    <animal name="DOG" />
    <animal name="CAT" />
    <extension>
    <instrument-list><![CDATA[ {"type":[{"name":"acoustic","instrument":[{"name":"GUITAR"},{"name":"VIOLIN"}]},{"name":"electronic","instrument":[{"name":"GUITAR"},{"name":"VIOLIN"}]}]} ]]></instrument-list>
    </extension>
    </home>

Is it violates standard and best practices ?

like image 655
hiddenuser Avatar asked Dec 03 '13 06:12

hiddenuser


1 Answers

There is no "XYZ specification" per say. Though I would recommend reading up on XML and its specification because you will need to take into consideration of escaping the following characters inside your JSON for things to work nicely with XML parsers.

XML escape characters.
"   &quot;
'   &apos;
<   &lt;
>   &gt;
&   &amp;

or use

<![CDATA[ ]]>
like image 109
Chad Avatar answered Sep 30 '22 17:09

Chad