Presentation is loading. Please wait.

Presentation is loading. Please wait.

Server-Side Application and Data Management IT IS 3105 (FALL 2009)

Similar presentations


Presentation on theme: "Server-Side Application and Data Management IT IS 3105 (FALL 2009)"— Presentation transcript:

1 Server-Side Application and Data Management IT IS 3105 (FALL 2009)
XML and PHP Mohamed Shehab Lecture 8

2 Working with XML The name XML comes from the full name of the language, Extensible Markup Language. XML is a method of data exchange, in that it holds well-defined content within its boundaries. XML Separates Data from HTML XML Simplifies Data Sharing XML Simplifies Data Transport XML Simplifies Platform Changes

3 <root> <child id=“1”> <subchild>
<root> <child id=“1”> <subchild>.....</subchild> </child> <child id=“2”> </root>

4 XML Structure XML documents contain two major elements: the prolog and the body. The prolog contains the XML declaration statement and any processing instructions and comments you want to add. The body contains the content structure is contained.

5 XML Structure (example.xml)
<?xml version="1.0" ?> <!-- Sample XML document --> <Books> <Book id=‘1’> <Author> Vikram Vaswani </Author> <Title> PHP A Beginner’s Guide </Title> <Edition> 1</Edition> <Year> 2009 </Year> <ISBN> </ISBN> </Book> </Books>

6 XML Structure There is only one root element in an XML document. In the previous example the root element is Books. All subsequent elements are referred to as children elements. XML Attributes provide additional information about elements. XML Attribute Values Must be Quoted XML is case sensitive, so <Book> and <book> are different elements. All XML tags must be properly closed, XML tags must be properly nested, and no overlapping tags are allowed.

7 Well Formed XML A "Well Formed" XML document has correct XML syntax.
The syntax rules: XML documents must have a root element XML elements must have a closing tag XML tags are case sensitive XML elements must be properly nested XML attribute values must be quoted

8 XML Parsing Methods Simple API for XML (SAX) parser: works by traversing an XML document sequentially, from beginning to end, and calling specific user-defined functions as it encounters different types of XML constructs. Document Object Model (DOM) parser: works by reading the entire XML document in one step and converting it to a hierarchical “tree” structure in memory. The parser can then be programmed to traverse the tree, jumping between “sibling” or “child” branches of the tree to access specific pieces of information.

9 SAX and DOM SAX reads XML data in “chunks” and is efficient for large files, but it requires the programmer to create customized functions to handle the different elements in an XML file. DOM requires less customization but can rapidly consume memory for its actions and so is often unsuitable for large XML data files. The choice of method thus depends heavily on the requirements of the application in question.

10 Using the SimpleXML PHP Extension
PHP supports both SAX and DOM parsing methods. The SimpleXML extension provides a user-friendly and intuitive interface to read and process XML data. SimpleXML represents every XML document as an object and turns the elements within it into a hierarchical set of objects and object properties. Accessing a particular element now becomes as simple as using parent->child notation to traverse the object tree until that element is reached.

11 XML Tree Structure <?xml version='1.0'?> <address>
<street>13 High Street</street> <county>Oxfordshire</county> <city> <name>Oxford</name> <zip>OX1 1BA</zip> </city> <country>UK</country> </address> address street county city name zip country

12 SimpleXML: Reading XML Tree
address street county city name zip country <?php $xml = simplexml_load_file('address.xml') or die (“Cant load XML!"); echo "City: " . $xml->city->name . "\n"; echo "Postal code: " . $xml->city->zip . "\n"; ?>

13 SimpleXML: Accessing Elements
Accessing elements within an XML document that contain characters not permitted under PHP's naming convention (e.g. the hyphen) can be accomplished by encapsulating the element name within braces and the apostrophe. <?xml version='1.0'?> <activity> <name> Business Meeting </name> <start-date>Mon, 21 Sep :30:00 EDT</start-date> <end-date>Mon, 21 Sep :30:00 EDT</end-date> </activity> <?php $xml = simplexml_load_file('activity.xml') or die (“Cant load XML!"); echo “Name: " . $xml->name . "\n"; echo “Start Date: " . $xml->{‘start-date’} . “<br>"; echo “End Date: " . $xml->{‘end-date’} . “<br>"; ?>

14 SimpleXML: Multiple element instances
library book title author year Multiple instances of the same element at the same level of the XML document tree are represented as arrays. Can easily be presented by the loops.

15 SimpleXML: Multiple element instances
library book title author year <?php $xml = simplexml_load_file(‘library.xml') or die (“Cant load XML!"); foreach ($xml->book as $book) { echo $book->title . “ is written by “ . $book->author . “<br>”; } ?>

16 SimpleXML: Counting multiple Elements
Use the count() function: <?php $xml = simplexml_load_file(‘library.xml') or die (“Cant load XML!"); echo count($xml->book) . ‘ book(s) found <br>’; ?>

17 SimpleXML: Reading Attributes
SimpleXML has an easy way to get to the attributes attributes and values are converted into keys and values of a PHP associative array and can be accessed like regular array elements.

18 SimpleXML: Reading Attributes
<?xml version="1.0"?> <library> <book id="1" genre="horror" rating="5"> <title>The Shining</title> <author>Stephen King</author> <pages>673</pages> </book> … </library> <?php $xml = simplexml_load_file(‘library.xml') or die (“Cant load XML!"); foreach ($xml->book as $book) { echo $book->title . “ is written by “ . $book->author . “ genre is ”. $book[‘genre’] . “ and rating is “ . $book[‘rating’] .”<br>”; } ?>

19 Reading RSS Files rss channel title link pubDate ttl item description


Download ppt "Server-Side Application and Data Management IT IS 3105 (FALL 2009)"

Similar presentations


Ads by Google