Presentation is loading. Please wait.

Presentation is loading. Please wait.

School of Computing and Information Systems CS 371 Web Application Programming XML and JSON Encoding Data.

Similar presentations


Presentation on theme: "School of Computing and Information Systems CS 371 Web Application Programming XML and JSON Encoding Data."— Presentation transcript:

1 School of Computing and Information Systems CS 371 Web Application Programming XML and JSON Encoding Data

2 School of Computing and Information Systems CS 371 Web Application Programming Data Encoding transporting data from script to another data needs to be organized fixed length, comma delimited or proprietary formats can be efficient but structure must be “known” by sender and receiver DBMS is not an option because they cannot be accessed by clients need a portable format where the structure is independent of the scripts

3 School of Computing and Information Systems CS 371 Web Application Programming Exensible Markup Language subset of SGML enforces well-formed tags (or elements) tags are not predefined users can define structure of data allows groups to agree to a standard and easily enforce it. tags are nested, self-described and case sensitive

4 School of Computing and Information Systems CS 371 Web Application Programming Data Flow XML document (data) XML document (data) DTD or schema Validator Sending script Receiving script Parser

5 School of Computing and Information Systems CS 371 Web Application Programming Structure of an XML Document elements (tags) and attributes like XHTML to be well-formed all elements must be terminated elements must be case sensitive elements must be properly nested must have a root element attribute values must be quoted white space is preserved declarative tag

6 School of Computing and Information Systems CS 371 Web Application Programming XML Example Thomas Cruise Saginaw Jack Nicholson Grand Rapids root tag attributesname and homeTown are children of student element text node

7 School of Computing and Information Systems CS 371 Web Application Programming Rules for Elements names composed of letters, numbers and other chars cannot start with a number or punctuation cannot start with xml (XML, etc) no spaces - also avoid "." can have content attributes attributes can have values values must be quoted

8 School of Computing and Information Systems CS 371 Web Application Programming Content normally everything is parsed (#PCDATA) built-in entities: &, <, " &xx; (&39; is a single quote) use to pass data through parser as simple text

9 School of Computing and Information Systems CS 371 Web Application Programming Examples well-formed Grand Valley text not quite well-formed first bullet second Grand Valley Jason Mikey

10 School of Computing and Information Systems CS 371 Web Application Programming Validation To be well-formed a document must also conform to a dtd (document type definition) dtd defines the structure of an xml doc: name of root element what is valid/invalid content for an element what attributes can be used type of data (parsed or unparsed) dtd can be internal or external xml schema is alternative to dtd many xml validators – just google it

11 School of Computing and Information Systems CS 371 Web Application Programming DTD Construction reference in xml file external: internal prologue in dtd file (

12 School of Computing and Information Systems CS 371 Web Application Programming DTD Construction (cont) defining elements: content models elements can be empty can have text can have other elements avoid use of ANY ex: student must have id, name and grade

13 School of Computing and Information Systems CS 371 Web Application Programming DTD Construction Recurrence comma means strict order | (pipe) is like or + is one or more, * is zero or more ? means that the element is optional use parens () for grouping

14 School of Computing and Information Systems CS 371 Web Application Programming DTD Construction Examples in the xml file contents can include any parsed data must have name, id, major and any # of minors name, any number of hobbies or sports and age contains parsed data or name and address

15 School of Computing and Information Systems CS 371 Web Application Programming DTD Attribute Types ex: type can be CDATAcharacter data (en1|en2|…)one from the list IDa unique identifier IDREFreferences an ID NMTOKENvalid XML name ENTITYvalue is an entity (&st;) a few others…

16 School of Computing and Information Systems CS 371 Web Application Programming DTD Attribute Types (cont) defVal (default value) can be: valuean actual value #REQUIREDattribute is required #IMPLIEDnot required #FIXEDattribute value is fixed

17 School of Computing and Information Systems CS 371 Web Application Programming Caveats hosting an xml file – make sure it has executable permissions

18 School of Computing and Information Systems CS 371 Web Application Programming XML Parsers parsers are used to traverse xml tree tree-based (DOM) loads entire document into memory random access, readable and writable contains objects: doc, node, nodeList, error stream-based (SAX) fires events when encountering new entity ex: fires startElement(“author”,attr) DOM and SAX parsers in.net, java, etc.

19 School of Computing and Information Systems CS 371 Web Application Programming xml parsers javaScript already familiar with dom parser (getElementsByTagName) see www.w3schools.com/xml/xml_parser.aspwww.w3schools.com/xml/xml_parser.asp no sax parser (why not?). (3 rd party) PHP dom parser – see php.net/manual/en/book.simplexml.php sax parser – see php.net/manual/en/book.xml.php

20 School of Computing and Information Systems CS 371 Web Application Programming XSL: Stylesheets for XML XSL is like a template used with an xml file like xhtml with special tags for trans. xml use XPATH for navigation select (student) // selects all child nodes select (/student[0]) // first student from root elements: template element defines scope of xml

21 School of Computing and Information Systems CS 371 Web Application Programming JSON basic syntax {“var1”:val,”var2”:val…} just like array initialization in javascript (hmm) in php use json_encode($object) to create a JSON string in javascript: var myObject = JSON.parse(text, reviver); where reviver is opt function called for every key and value


Download ppt "School of Computing and Information Systems CS 371 Web Application Programming XML and JSON Encoding Data."

Similar presentations


Ads by Google