Thursday, June 17, 2010

Compact xml

Xml is one of most popular format for storing and exchanging data.
For example if we want to store address in an xml file then it would look some thing as below
<AddressBook>
  <Contact>
    <Name>Person1</Name>
    <Address1><![CDATA[Number101]]></Address1>
    <Address2><![CDATA[Address501]]></Address2>
    <Zip>50001</Zip>
    <Phone><![CDATA[999991]]></Phone>
 </Contact>
</AddressBook>
If we use the xml format to store the data and exchange data in an internal or intranet application then consider changing the format compact as below
<AB>
  <C>
    <N>Person1</N>
    <A1><![CDATA[Number101]]></A1>
    <A2><![CDATA[Address501]]></A2>
    <Z>50001</Z>
    <P><![CDATA[999991]]></P>
 </C>
</AB>
Keeping the tag name shot reduces the overall xml file size. Also have bandwidth advantage if transfered across the network.
Create an xml file with normal tag names with 10000 Contact nodes. Create another xml file with short tag names with 10000 C nodes.
Output:
The size of the xml file with normal tag name is approximately 2.08 MB.
The size of the xml file with short tag name is approximately 1.57 MB.
Below is an example of xml file with short attribute name.
<AB>
  <C N="Person1" Z="50001">
    <A1><![CDATA[Number101]]></A1>
    <A2><![CDATA[Address501]]></A2>
    <P><![CDATA[999991]]></P>
 </C>
</AB>

Rule: Consider short names for xml tags (element name or node name) and attributes.

No comments: