CS 318 - Week 14 Lecture - 5-1-13

*   a few words on XML and JSON

*   start with XML
    *   XML - EXtensible Markup Language

    *   XML was designed to describe data
        and to focus on what data is

    *   Sebesta: disagrees that XML is a markup
        language -- claims: it is a META-markup
        language that specifies rules for creating
	markup languages

    *   XML tags are not predefined; you define
        your own tags;

        *   XML uses a Document Type Definition (DTD)
	    or an XML Schema to then record 
	    agreed-upon definitions for a data-
	    description "language"

    *   XML doesn't really "do" anything;l
        it is a means for describing data;

    *   plain-text, with angle-bracket delimited
        tags;

    *   has been pretty extensively used;

XML syntax:
*   strict, relatively simple
*   begins with an XML prologue,
    *   declares that this is an XML document
    *   defines the version and character encoding
        in use
    *   then there is a DTD declaration/doctype
        or a reference to an XML schema

    <?xml version="1.0" encoding="ISO-8859-1" ?>

    ^ this could be followed by a DTD declaration
    doctype or a reference to an XML schema:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
    "http://www.w3.or/TR/shtml11/DTD/xhtml11.dtd">

*   this prologue is followed by a root tag (Stepp
    et al call a document tag), a single, outermost
    tag that encloses ALL of the rest of the
    content of the document

    ALSO...

    *   all tags that are opened must be closed

    *   tags are case sensitive (and opening,
        closing tags MUST match in terms of case)

    *   proper nesting is required

    *   tags can have attributes,
        attribute values must be quoted

*   basically, XML elements make up a tree!
    the document/root element is its root,
    there are children, parents,
              ancestors, descendants,
	      the usual "tree" vocabulary;

               ^ XML elements have relationships

*   you can also talk about the kind of
    CONTENT an element can have:

    *   element content (an element can have an element
        or elements nested within it)
    *   simple content (just text, numbers, etc.
    *   empty content
    *   mixed content - a combination of element
        content and simple content

*   elements can have attributes --
    but those aren't considered content...

*   XML with correct syntax is WELL-FORMED XML
    XML validated against a DTD or Schema is
        VALID XML

*   Intro to JSON

    JavaScript Object Notaton 

    *   textbook, p. 468: "A technique for exchanging
        data by encoding it as text similar to
	the declaration of a set of JavaScript
	objects"

    *   XML is versatile, it is a useful
        data exchange format, BUT...
	
	many find its syntax to be bulky,
        it can be a chore to "walk through"
	an XML Document Object Model (DOM)
	to get to the data you want to process;

        JSON is an effort to improve on the
	above;

    *   JavaScript developer Douglas Crockford
        came up with a novel way of exchanging
	data that he dubbed JavaScript Object
	Notation, or JSON for short --

        *   IS closely tied to the syntax
	    and usage of JavaScript, but CAN be
	    used in other languages and environments
	    as well;

	*   text notes that all modern web browsers
	    have native JSON support, and there
	    are support libraries for older
	    browsers;

        *   it is also a plain-text format

            is human readable,
	    and self-describing,
	    allows for hierarchical data,
	    and can be easily transferred
	    between applications or across
	    the net or etc.

*   first: example of a JavaScript object:

    *   unlike C++/Java, you don't have to
        create a class first to get an object --
	you can just make an object!

    var person =
    {
        name: "Philip J. Fry",
        age: 23,
        weight: 172.5,
        friends: ["Farnsworth", "Hermes", "Zoidberg"],
        getBeloved: function()
                    {
                        return this.name + 
                               " loves Leela";
                    }
    };

   alert(person.age);       // 23
   alert(person["weight"]); // 172.5
   alert(person.friends[2]); // Zoidberg
   alert(person.getBeloved()); // Philip J. Fry loves Leela

   *   note that a JavaScript object's properties
       can be any kind of data, including
       functions

JSON data format

*   very much based on this JavaScript object
    syntax;

{
    "messages":
    [
        {
            "time": "1.57",
            "sender": "Jessica Miller",
            "body": "So do we have a titl for the book yet?"   
        },
        {
            "time": "1:58",
            "sender": "Marty Stepp",
            "body": "I was hoping for a pun?"   
        },
        {
            "time": "2:03",
            "sender": "Jessica Miller",
            "body": "sheesh"   
        }
    ]
}

    *  JSON's data format is not EXACTLY the
       same as JavaScript's object notation --

       *   for example, ALL object property names
           must be enclosed in quotes

       *   JSON objects may not have functions
           as properties

       *   some characters are forbidden as JSON
           data for compatibility reasons

JSON.parse(myJSONdata) -> returns that JSON
		          data converted into an
			  equivalent JavaScript
			  object

JSON.stringify(myJavaScriptObject) -> returns
		          that JavaScript object
			  converted into
			  equivalent JSON data

another example:

{
   "firstName": "Billy",
   "lastName": "Gates",
   "age": 12,
   "address": "1234 5th Street",
   "phoneNumbers:" ["888-555-1234", "888-555-5678"]
}

...is XML dead?
*   no -- it's still used for many applications
    (web AND non-web),
    it is more easily validated against schemas/DTDs
    so it probably more suitable for complex data;

    BUT JSON's brevity, simplicity, and ease of use
    make it very attractive as well