XML and JSON
JSON differs from XML in that
* has no ending tag * shorter * quicker to write * can be parsed using built-in JS eval() * uses arrays * no reserved words
What kind of data can XML mark up?
Any kind of data!
How is XML extensable?
It can be used for both documents and messages. Unlike HTML, new "tags" can be defined
getJSON()
a function included in the JQuery library
Using JSON on Web Browser with JS
after fetching JSON data from a web server: * convert JSON data into JS object
JSON objects
are written inside curly brackets and can contain multiple name/value pairs
JSON syntax
data is in name/vaule pairs data is separated by commas curly braces hold objects square brackets hold arrays
XML Schema
describes the structure of an XML doc the XML Schema language is reffered to XML Schema Definition (XSD)
What does XML stand for? What does XML do?
eXtensible Markup Language XML defines a set of rules for encoding documents in a format that is human-readable and machine-readable
An XML doc should start with
xml declaration doctype declaration processing instructions
well formatted XML
- element names are composed of valid chars (invalid: &, <, >, ", '. For these, use XML entities) - element names can't start with a number - there must be a single root element - all elements must have a closing brace ( or be self closing) - elements can contain attributes - attribute values must be in quotes - element and attribute names are case sensitive
XML Schema pros
- it's easier to describe allowable doc content - it's easier to validate the correctness of data - it's easier to define data facets - it's easier to define data patterns
What does XML do?
-separates data from HTML -simplifies data sharing -simplifies data transport -simplifies platform changes
DTD examle
<?xml version="1.0" encoding="blah"?> <!DOCTYPE art [ <!ELEMENT art (painting*)> <!ELEMENT PAINTING (title, artist, year, medium)> <!ATTLIST painting id CDATA #REQUIRED> <!ELEMENT title (#PCDATA)> <!ELEMENT artist (name, nationality)> <!ELEMENT name (#PCDATA)> <!ELEMENT nationality (#PCDATA)> <!ELEMENT year (#PCDATA)> <!ELEMENT medium (#PCDATA)> ]> <art> ... </art>
Eval()
A built in JS function used to convert JSON data into a JS object can compile and execute any JS presents a security risk so DON'T USE IT
what are the building blocks of an XML doc?
Elements Attributes Entities PCDATA - parsed character data which is parsed and passed to HTML CDATA - (unparsed) character data, just passed to the HTML; EX: <tag>![CDATA[blah blah blah]]</tag>
HTML vs. XML
HTML -defines visual document layout XML -defines semantic structure for data
What is JSON?
JavaScript Object Notation it is a data serialization format and is more concise than XML used in many REST (Representational state transfer) APIs
DTD
Tells XML parser which elements and attributes to expect in the doc as well as the order and nesting of those elements can be defined w/in an XML doc or in external file
XSLT
XML Stylesheet Transformations used for transforming XML into other doc formats used both server-side an w/in JavaScript XML parser needed to perform the actual transformations
Valid XML
XML doc that is well formed and whose element and content conform o the rules of its DTD or its schema
XPath
XPath is a standardized syntax for searching an XML doc and for navigating to elements within the XML doc XPath is typically used as part of the programmatic manipulation of an XML document in PHP and other languages XPath uses a syntax that is similar to the one used in most operating systems to access directories.
JSON arrays
are written inside square brackets can contain multiple objects
What is one of the key benefits of XML
as plain text it can be transferred b/w different apps and OSs and is human-readable/understandable
Name/value pairs consists of
fieldname (in double quotes) followed by a colon folowed by a vallue that can be: * number (int or float) * string (in double quotes) * boolean (true/false) * array (in square brackets) * object (in curly brackets) * null
XML Processing in-memory approach
involves reading the entire XML file into memory into some type of data structure w/ functions for accessing and manipulating data
JSON is similar to XML in that
it is plain text it is "self-describing" (human readable) it is hierarchical it can be parsed by JS can be transported using AJAX
From what is JSON originally derived?
it was originally derived from the JS scripting language BUT!!! JSON is a language-independent data format
XML Processing event or pull approach
lets you pull in just a few elements or lines at a time, thereby avoiding the memory load of large XML file
JSON.Parse()
natively supported by browser * recongnize only JSON text and will not compile scripts * added to ECMAScript 5th E Standard and is now supported by the major browsers
How is JSON primarily used?
to transmit data b/w a server and web app.