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 (Part 2) Mohamed Shehab Lecture 8

2 Converting XML to SQL In some cases you need to convert an XML document to an SQL table. The steps: Read the different XML elements/attributes Store the read values into the SQL table.

3 Converting XML to SQL (Cont.)
library book title author pages <library> <book id="1" genre="horror" rating="5"> <title>The Shining</title> <author>Stephen King</author> <pages>673</pages> </book> <book id="2" genre="suspense" rating="4"> <title>Shutter Island</title> <author>Dennis Lehane</author> <pages>390</pages> </library> id genre rating title author pages 1 horror 5 The Shining Stephen King 673 2 suspense 4 Shutter Island Dennis Lehane 390

4 Converting XML to SQL (Cont.)
library book title author pages <?php $xml = simplexml_load_file(‘library.xml') or die (“Cant load XML!"); foreach ($xml->book as $book) { $query = “INSERT INTO library (id, genre, rating, title, author, pages) VALUES (‘$book[‘id’]’,’$book[‘genre’]’,’$book[‘rating’]’, ’$book->title’,’$book->author’,’$book->pages’)”; }?>

5 Altering Element and Attributes
To alter an elements and attributes: <?php $xml = simplexml_load_file(‘library.xml') or die (“Cant load XML!"); $xml->book[1]->title = ‘Invisible Prey’; $xml->book[1]->author = ‘John Sandford’; $xml->book[1]{‘rating’} = 5; header(‘Content-Type: text/xml’); echo $xml->asXML(); ?>

6 Adding New Elements and Attributes
We use the simplexml method addChild() to add an element. We use the simplexml method addAttribute() to add and attribute. <?php $xml = simplexml_load_file(‘library.xml') or die (“Cant load XML!"); $book = $xml->addChild(‘book’); $book->addAttribute(‘genre’,’travel’); $book->addAttribute(‘rating’,4); $book->addChild(‘title’, ‘Frommer Italy 2007’); $book->addChild(‘author’, ‘Various’); $book->addChild(‘pages’, 850); header(‘Content-Type: text/xml’); echo $xml->asXML(); ?>

7 Adding the id Attribute?
We need to find the next id to give the new object: <?php $xml = simplexml_load_file(‘library.xml') or die (“Cant load XML!"); $numBooks = count($xml->book); $lastID = $xml->book[($numBooks - 1)]{‘id’}; $book = $xml->addChild(‘book’); $book->addAttribute(‘genre’,’travel’); $book->addAttribute(‘rating’,4); $book->addAttribute(‘id’,($lastID + 1)); $book->addChild(‘title’, ‘Frommer Italy 2007’); $book->addChild(‘author’, ‘Various’); $book->addChild(‘pages’, 850); header(‘Content-Type: text/xml’); echo $xml->asXML(); ?>

8 Creating New XML Documents
You can create an new empty XML document then use addChild() and addAttribute() methods. <?php $xmlStr = "<?xml version='1.0'?><person></person>"; $xml = simplexml_load_string($xmlStr); $xml->addAttribute('age', '18'); //adding attributes to the person $xml->addAttribute('sex', 'male'); $xml->addChild('name', 'John Doe'); //adding child element to the person $xml->addChild('dob', ' '); $address = $xml->addChild('address'); $address->addChild('street', '12 A Road'); //adding child element to address $address->addChild('city', 'London'); $country = $address->addChild('country', 'United Kingdom'); $country->addAttribute('code', 'UK'); //adding attribute to address header('Content-Type: text/xml'); echo $xml->asXML(); ?>


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

Similar presentations


Ads by Google