11. Objects and Portable Data: XML and JSON


Objects and Portable Data: XML and JSON

XML and JSON are standard mechanisms for defining and transporting data between potentially disparate systems. By using XML or JSON a system is not limited to only communicating with systems written in the same language. XML/JSON allow systems to interact with systems regardless of what language they were written in. The Extensible Markup Language(XML)

XML and HTML are descendants of SGML, the Standard Generalized Markup Language. While HTML is used to present data in a browser, XML concerns itself with data verification. In XML you must create your own tags and define the type of data that is accepted in the tags. This allows you to verify that the data you received is valid.

Validating the Document with the Document Type Definition (DTD)

XML allows you to validate your data with a document called the Document Type Definition(DTD). This document sets the rules for the tags that must be used and it also tells us which data must be parsed. Using a DTD is optional and not necessary but definitely recommened.

<!-- DTD for supplier document -->
<!ELEMENT supplier ( name, address ) >
<!ELEMENT name ( companyname) >
<!ELEMENT companyname ( #PCDATA) >
<!ELEMENT address ( street+, city, state, zip ) >
<!ELEMENT street (#PCDATA) >
<!ELEMENT city (#PCDATA) >
<!ELEMENT state (#PCDATA) >
<!ELEMENT zip ( #PCDATA) >

The DTD will be expecting XML data in the following format:

<!-- Expected Template based on the DTD sheet -->
<supplier>
    <name>
        <companyname>My Company</companyname>
    </name>
    <address>
        <street>123 Bagel Street</street>
        <city>Los Angeles</city>
        <state>California</state>
        <zip>96520</zip>
    </address>
</supplier>

There is Internal and External DTD documents types, and the difference is whether the DTD is being defined in the document which it will be used. Internal DTD documents are defined in the same document where it is going to be used. External DTD documents are defined in their own document and included in the document where they will be used. Internal and External DTD have the same syntax and will will use External DTD for this example.

The 1st Line in the example is a comment and like any other computer language, those are ignored and only used to add descriptive notes for other developers.
The 2nd and 3rd Line defines the outer tags and within the paranthasis is the name of the inner tags that are expected.
The 4th Line defines the tag which will have the data that will be parsed. The #PCDATA is a representation of the data that will be parsed. PCDATA stand for Parsed Character Data.

Javascript Object Notation (JSON)

Although XML is much more structured, especially when using a DTD, Javascript Object Notation is more flexible. An important point to make is that JSON used the same syntax used to create objects.

{
    "supplier": [
        { 
           "companyname": "My Company",
           "street": "123 Bagel Street",
           "city": "Los Angeles",
           "state": "Califorina",
           "zip": "96520"
         },
        { 
           "companyname": "My Second Company",
           "street": "321 Coffee Street",
           "city": "New York City",
           "state": "Califorina",
           "zip": "02569"
         }
      ]
}

The w3schools site provides the following bullet itme description of JSON:

  • JSON is lightweight text-data interchange format.
  • JSON is language independent.
  • JSON is “self-describing” and easy to understand.

Using the Formal Public Identifier (FPI) and Implementing the DTD

<!-- This includes the DTD which we will be validating against. -->
<?xml version="1.0" standalone="no"?>
<!DOCTYPE supplier SYSTEM "supplier.dtd">

<!-- This will be the data that we will be validating. -->
<supplier>
    <name>
        <companyname>My Company</companyname>
    </name>
    <address>
        <street>123 Bagel Street</street>
        <city>Los Angeles</city>
        <state>California</state>
        <zip>96520</zip>
    </address>
</supplier>

The lines that we need to pay attention to here is the first 2 uncommented lines.

  • The 1st line is just declaring the XML file, which version it is using and whether there will be any other files added.
  • The 2nd line states the Document Type, the Root XML Element, the type of path to the DTD, and the path to the actual DTD
<!-- Declaring and XML document, the version being used, 
and whether it will be any other files used. -->
<?xml version="1.0" standalone="no"?>

<!-- The basic syntax of a Formal Public Identifier (FPI) -->
<DocumentType RootElement TypeOfPath PathOfDTD>

In Conclusion

This was a brief summary of XML and how to use it. There is different type of elements and I would recommend on doing a little bit more research just to see what else is available.

If you would like more example on how to implement DTD and XML, Click Here
If you would like more information on XML,Click Here


Download Lesson Notes

The Object-Oriented Thought Process

Author: Matt Weisfeld