0% found this document useful (0 votes)
14 views34 pages

05 - XML

Uploaded by

SY010
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views34 pages

05 - XML

Uploaded by

SY010
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

eXtensible Markup

Language (XML) /
JSON
Chapter 19

Randy Connolly and Ricardo Hoar Fundamentals of Web Development


Textbook to be published by Pearson ©
Ed2015
in early
Pearson
2014
Randy Connolly and Ricardo Hoar Fundamentals of Web Development
http://www.funwebdev.com
Objectives
1 XML Overview
2 DTD: Document Type
Definition

3 XSD: XML Schema


Document 4 JSON

5 Self-Reading: XPATH
6 Self-Reading: XML
Processing

7 Sef-Reading:
Self Reading: XML Style
XML Processing
Transformations

Randy Connolly and Ricardo Hoar Fundamentals of Web Development


Section 1 of 7

XML OVERVIEW
Randy Connolly and Ricardo Hoar Fundamentals of Web Development
XML Overview
• Developed from SGML
• Became a W3C Recommendations in 1998
• A meta-markup language -> unlike HTML, XML can be used to mark
up any type of data. Tags are not predefined (user generated)
• Deficiencies of HTML and SGML
• Many complex features that are rarely used
• HTML is a markup language, XML is used to define markup languages
• Markup languages defined in XML are known as applications
• XML can be written by hand or generated by computer
• Useful for data exchange
• Foundation for several next-generation web technologies:
• RSS, AJAX, Web Services, etc.

Randy Connolly and Ricardo Hoar Fundamentals of Web Development


XML Overview
One of the key benefits of XML data is that it is plain text: it
can be read and transferred between applications and
different operating systems as well as being human-
readable and understandable as well.
XML is used to structure and describe information.
XML is not only used on the web server and to
communicate asynchronously with the browser, but is also
used as a data interchange format for moving information
between systems.

Randy Connolly and Ricardo Hoar Fundamentals of Web Development


XML Overview
Used in many systems

Randy Connolly and Ricardo Hoar Fundamentals of Web Development


XML Advantages and
Disadvantages
Pros
Human-readable, self-documenting format
Strict syntax allows standardized tools
International, platform-independent
Can represent almost any general kind of data (record, list, tree)
Cons
Bulky syntax/structure makes files large; can decrease
performance

Randy Connolly and Ricardo Hoar Fundamentals of Web Development


An Example XML Application
<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Ana</to>
<from>Dana</from>
<subject>Reminder</subject>
<message>
Don't forget me this weekend!
</message>
</note>

Randy Connolly and Ricardo Hoar Fundamentals of Web Development


XML File Structure
• A header, then a single document tag that can contain
other tags
<?xml version="1.0" encoding="UTF-8"?>

• Tag syntax:
<element attributes> text or tags </element>

• Attribute syntax:
name="value"

• comments:
<!-- comment -->

Randy Connolly and Ricardo Hoar Fundamentals of Web Development


Well Formed XML
For a document to be well-formed XML, it must follow the syntax
rules for XML:
• Element names are composed of any of the valid characters
(most punctuation symbols and spaces are not allowed) in XML.
• Element names can’t start with a number.
• There must be a single-root element. A root element is one that
contains all the other elements; for instance, in an HTML
document, the root element is <html>.
• All elements must have a closing element (or be self-closing).
• Elements must be properly nested.
• Elements can contain attributes.
• Attribute values must always be within quotes.
• Element and attribute names are case sensitive.

Randy Connolly and Ricardo Hoar Fundamentals of Web Development


Well Formed XML
Sample Document

Randy Connolly and Ricardo Hoar Fundamentals of Web Development


Valid XML
A valid XML document is one that is well formed and whose
element and content conform to the rules of a certain schema

Schema: describes the structure of an XML


document, by setting rules specifying which tags
and attributes are valid, and how they can be
used together
Used to check XML files to make sure they follow
the rules set in the schema

Two ways to define a schema:


Document Type Definition (DTD)
W3C XML Schema

Randy Connolly and Ricardo Hoar Fundamentals of Web Development


Valid XML

Randy Connolly and Ricardo Hoar Fundamentals of Web Development


Section 2 of 7

DTD: DOCUMENT TYPE


DEFINITION
Randy Connolly and Ricardo Hoar Fundamentals of Web Development
DTD
Document Type Definition

A DTD tells the XML parser which elements and attributes


to expect in the document as well as the order and nesting
of those elements.
• A set of structural rules called declarations
• Define tags, attributes, entities
• Specify the order and nesting of tags
• Specify which attributes can be used with which tags.
• DTD can be:
• Embedded inside XML (internal DTD).
• Stored in a separate file (external DTD saved as .dtd).

Randy Connolly and Ricardo Hoar Fundamentals of Web Development


DTD: Declarations

General syntax for declarations


<!keyword …. >

Note, not XML!

Four possible keywords:


ELEMENT, for defining tags

ATTLIST, for specifying attributes in your tags

ENTITY, for identifying sources of data

NOTATION, for defining data types for non-XML data

Randy Connolly and Ricardo Hoar Fundamentals of Web Development


DTD: Elements
• General syntax
• <!ELEMENT element-name (content-description)>

• Content description specifies what tags may appear inside the named
element and whether there may be any plain text in the content
• EX: <!ELEMENT person (parent+, age, spouse?, sibling*)>
• An element can be either an internal or a leaf node.
• Multiplicity
• +
• *
• ?
• Leaf elements can be:
• #PCDATA
• EMPTY
• ANY

Randy Connolly and Ricardo Hoar Fundamentals of Web Development


DTD: Attributes
• Declaring attributes general syntax:
<!ATTLIST element-name
(attribute-name attribute-type default-value?)+ >

• There are 10 attribute types, only CDATA will


be used.
• Default values
• A value
• #FIXED value
• #REQUIRED (no default value, each instance
must specify value)
• #IMPLIED (default, if not specified)

Randy Connolly and Ricardo Hoar Fundamentals of Web Development


Data Type Definition
Example

Randy Connolly and Ricardo Hoar Fundamentals of Web Development


XML Entities
XML document may be distributed among a number of files
• Each unit of information is called an entity
• Each entity has a name to identify it
• Defined using an entity declaration
• Used by calling an entity reference
Declared in DTD
<!DOCTYPE My_XML_Doc [
<!ENTITY name "replacement">
]>

<!ENTITY xml "eXtensible Markup


Language">
The &xml; includes entities
The eXtensible Markup Language includes entities

Randy Connolly and Ricardo Hoar Fundamentals of Web Development


Attributes or Elements
There are no rules about when to use attributes
and when to use elements.
Avoid XML Attributes?
Some of the problems with using attributes are:
• attributes cannot contain multiple values (elements can)

• attributes cannot contain tree structures (elements can)

• attributes are not easily expandable (for future changes)

Randy Connolly and Ricardo Hoar Fundamentals of Web Development


DTD: Internal or External

A document type declaration can either contain declarations


directly or can refer to another file
Internal
<!DOCTYPE root-element [
declarations
]>
External file
<!DOCTYPE root-name SYSTEM “file-name”>

Randy Connolly and Ricardo Hoar Fundamentals of Web Development


Data Type Definition

The main drawback with DTDs is that they can only


validate the existence and ordering of elements. They
provide no way to validate the values of attributes or
the textual content of elements.
For this type of validation, one must instead use XML
schemas, which have the added advantage of using
XML syntax. Unfortunately, schemas have the
corresponding disadvantage of being long-winded and
harder for humans to read and comprehend.

Randy Connolly and Ricardo Hoar Fundamentals of Web Development


Section 3 of 7

XSD: XML SCHEMA


Randy Connolly and Ricardo Hoar Fundamentals of Web Development
XML Schema
Just one example

Randy Connolly and Ricardo Hoar Fundamentals of Web Development


Section 4 of 7

JSON
Randy Connolly and Ricardo Hoar Fundamentals of Web Development
JSON

JSON stands for JavaScript Object Notation (though its


use is not limited to JavaScript)
Like XML, JSON is a data serialization format. It
provides a more concise format than XML.
Many REST web services encode their returned data in
the JSON data format instead of XML.

Randy Connolly and Ricardo Hoar Fundamentals of Web Development


JSON
An example XML object in JSON

Randy Connolly and Ricardo Hoar Fundamentals of Web Development


JSON
An example XML object in JSON

Randy Connolly and Ricardo Hoar Fundamentals of Web Development


Using JSON in JavaScript
Creating JSON JavaScript objects

it is easy to make use of the JSON format in JavaScript:


var a = {"artist": {"name":"Manet","nationality":"France"}};
alert(a.artist.name + " " + a.artist.nationality);
When the JSON information will be contained within a string
(say when downloading) the JSON.parse() function can be used
to transform the string containing into a JavaScript object

Randy Connolly and Ricardo Hoar Fundamentals of Web Development


Using JSON in JavaScript
Convert string to JSON object and vice versa

var text = '{"artist": {"name":"Manet","nationality":"France"}}';


var a = JSON.parse(text);
alert(a.artist.nationality);
JavaScript also provides a mechanism to translate a JavaScript
object into a JSON string:
var text = JSON.stringify(artist);

Randy Connolly and Ricardo Hoar Fundamentals of Web Development


Using JSON in PHP
JSON on the server

Converting a JSON string into a PHP object is quite


straightforward:

Notice that the json_decode() function can return either a PHP


object or an associative array.

Randy Connolly and Ricardo Hoar Fundamentals of Web Development


Using JSON in PHP
Go the other way

To go the other direction (i.e., to convert a PHP object into a


JSON string), you can use the json_encode() function.

Randy Connolly and Ricardo Hoar Fundamentals of Web Development


Using JSON in PHP
JSON on the server

Since JSON data is often coming from an external source, always


check for parse errors before using it, via the json_last_error()
function:

Randy Connolly and Ricardo Hoar Fundamentals of Web Development

You might also like