xml for php developers
TRANSCRIPT
![Page 1: XML For PHP Developers](https://reader034.vdocuments.net/reader034/viewer/2022052315/554abe9ab4c90580698b4a2d/html5/thumbnails/1.jpg)
XML For PHP Developersby
Sudheer Satyanarayanahttp://techchorus.net
Bangalore PHP User Group Meetup30 October 2010
![Page 2: XML For PHP Developers](https://reader034.vdocuments.net/reader034/viewer/2022052315/554abe9ab4c90580698b4a2d/html5/thumbnails/2.jpg)
Agenda
Quick introduction to XML basicsRecipe 1 - parsing RSS feedRecipe 2 - creating Atom feedRecipe 3 - scraping information from websites using XMLQuestion and answer
At the end of the session, you will be able to start using XML with PHP.
![Page 3: XML For PHP Developers](https://reader034.vdocuments.net/reader034/viewer/2022052315/554abe9ab4c90580698b4a2d/html5/thumbnails/3.jpg)
XML Basics
XML document has a tree structure XML documents can be validated using an XML schemaAll major programming languages support reading and writing XML documentsHundreds of technologies are built on top of XMLXHTML is one of the XML markup languages
![Page 4: XML For PHP Developers](https://reader034.vdocuments.net/reader034/viewer/2022052315/554abe9ab4c90580698b4a2d/html5/thumbnails/4.jpg)
XML Parsers In PHP
simplexmlDOMSAXXMLReaderXMLWriter
![Page 5: XML For PHP Developers](https://reader034.vdocuments.net/reader034/viewer/2022052315/554abe9ab4c90580698b4a2d/html5/thumbnails/5.jpg)
Which Parser To Use
simplexml - really simple XML documentsDOM - heavy liftingSAX - large XML documentsXMLReader - large XML documents
Don't use:string manipulation to create XML documentsregular expressions to parse XML documents
![Page 6: XML For PHP Developers](https://reader034.vdocuments.net/reader034/viewer/2022052315/554abe9ab4c90580698b4a2d/html5/thumbnails/6.jpg)
Recipe 1
simplexml usage Programmatically retrieve weather information in your locationUsing Yahoo! Weatherparse RSS feed
![Page 7: XML For PHP Developers](https://reader034.vdocuments.net/reader034/viewer/2022052315/554abe9ab4c90580698b4a2d/html5/thumbnails/7.jpg)
RSS - Really Simple Syndication
![Page 8: XML For PHP Developers](https://reader034.vdocuments.net/reader034/viewer/2022052315/554abe9ab4c90580698b4a2d/html5/thumbnails/8.jpg)
Sample RSS document <?xml version="1.0" encoding="ISO-8859-1" ?><rss version="2.0">
<channel><title>My Home Page</title><link>http://www.example.com</link><description>My RSS sample page</description><item><title>RSS Tutorial</title><link>http://www.example.com/rss</link><description>New RSS tutorial</description></item><item><title>XML Tutorial</title><link>http://www.example.com/xml</link><description>New XML tutorial</description></item></channel></rss>
![Page 9: XML For PHP Developers](https://reader034.vdocuments.net/reader034/viewer/2022052315/554abe9ab4c90580698b4a2d/html5/thumbnails/9.jpg)
Retrieving weather info from Yahoo!
http://weather.yahooapis.com/forecastrss?w=2442047&u=c w = location, WOEIDu = degrees units (Fahrenheit or Celsius)http://weather.yahooapis.com/forecastrss?w=2295420&u=c
![Page 10: XML For PHP Developers](https://reader034.vdocuments.net/reader034/viewer/2022052315/554abe9ab4c90580698b4a2d/html5/thumbnails/10.jpg)
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?><rss version="2.0"xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0"><channel><item><title>Conditions for Bangalore, IN at 8:30 pm IST</title><description><![CDATA[<img src="http://l.yimg.com/a/i/us/we/52/27.gif"/><br /><b>Current Conditions:</b><br />Mostly Cloudy, 24 C<BR /><BR /><b>Forecast:</b><BR />Wed - Partly Cloudy. High: 30 Low: 19<br />Thu - Sunny. High: 33 Low: 20<br /><br />]]></description></item></channel></rss>
![Page 11: XML For PHP Developers](https://reader034.vdocuments.net/reader034/viewer/2022052315/554abe9ab4c90580698b4a2d/html5/thumbnails/11.jpg)
Visualization
![Page 12: XML For PHP Developers](https://reader034.vdocuments.net/reader034/viewer/2022052315/554abe9ab4c90580698b4a2d/html5/thumbnails/12.jpg)
PHP Code To Parse The Feed<?php
$url = 'http://weather.yahooapis.com/forecastrss?w=2295420&u=c';
$document = file_get_contents($url);
$xml = new SimpleXMLElement($document);
$output = $xml->channel->item->description;
$text_output = strip_tags($output);
echo $text_output;
![Page 13: XML For PHP Developers](https://reader034.vdocuments.net/reader034/viewer/2022052315/554abe9ab4c90580698b4a2d/html5/thumbnails/13.jpg)
Output
Current Conditions:Haze, 22 CForecast:Tue - Partly Cloudy. High: 26 Low: 19Wed - Isolated Thunderstorms. High: 27 Low: 19
Full Forecast at Yahoo! Weather(provided by The Weather Channel)
![Page 14: XML For PHP Developers](https://reader034.vdocuments.net/reader034/viewer/2022052315/554abe9ab4c90580698b4a2d/html5/thumbnails/14.jpg)
Atom
![Page 15: XML For PHP Developers](https://reader034.vdocuments.net/reader034/viewer/2022052315/554abe9ab4c90580698b4a2d/html5/thumbnails/15.jpg)
Atom - key takeaways
Disagreements in RSS community Atom entry documentAtom feed documentAtom Publishing ProtocolUse Atom if you are a feed publisher
![Page 16: XML For PHP Developers](https://reader034.vdocuments.net/reader034/viewer/2022052315/554abe9ab4c90580698b4a2d/html5/thumbnails/16.jpg)
Sample Atom Feed<?xml version="1.0"?><feed xmlns="http://www.w3.org/2005/Atom"><link href="http://www.meetup.com/Bangalore-PHP-Users/" rel="self"/><link href="http://www.meetup.com/Bangalore-PHP-Users/feed/atom.html" rel="alternate"/><title>PHP User Group Meet</title><id>http://example.com/my_unique_id.xml</id><subtitle>Bangalore PHP Users</subtitle><updated>2010-10-23T10:30:02Z</updated><entry><link href="http://www.meetup.com/Bangalore-PHP-Users/calendar/15022884/atom.xml" rel="self"/><link href="http://www.meetup.com/Bangalore-PHP-Users/calendar/15022884/atom.html" rel="alternate"/><title>October 2010 Meet</title><summary>Great meetup</summary><id>http://example.com/my_uique_feed_id.xml</id><updated>2010-10-27T10:30:02Z</updated><author><name>Sudheer Satyanarayana</name></author></entry></feed>
![Page 17: XML For PHP Developers](https://reader034.vdocuments.net/reader034/viewer/2022052315/554abe9ab4c90580698b4a2d/html5/thumbnails/17.jpg)
Atom Feed Structure
![Page 18: XML For PHP Developers](https://reader034.vdocuments.net/reader034/viewer/2022052315/554abe9ab4c90580698b4a2d/html5/thumbnails/18.jpg)
Atom Structure
<link href="..." rel="self"><link href="..." rel="alternate"><title>...</title><id>...</id><summary>...</summary><update>...</updated><author><name>...</name></author>
![Page 19: XML For PHP Developers](https://reader034.vdocuments.net/reader034/viewer/2022052315/554abe9ab4c90580698b4a2d/html5/thumbnails/19.jpg)
Recipe 2
Generate an Atom feedPHP DOM
![Page 20: XML For PHP Developers](https://reader034.vdocuments.net/reader034/viewer/2022052315/554abe9ab4c90580698b4a2d/html5/thumbnails/20.jpg)
Create An Atom Feed Document
<?php
$doc = DOMDocument::loadXML('<feed/>');
$root = $doc->documentElement;
$root->setAttribute('xmlns','http://www.w3.org/2005/Atom');
![Page 21: XML For PHP Developers](https://reader034.vdocuments.net/reader034/viewer/2022052315/554abe9ab4c90580698b4a2d/html5/thumbnails/21.jpg)
Create Link Nodes
$node = $doc->createElement('link');$link = $root->appendChild($node);$link->setAttribute('href', 'http://www.meetup.com/Bangalore-PHP-Users/');$link->setAttribute('rel', 'self');
$node = $doc->createElement('link');$link = $root->appendChild($node);$link->setAttribute('href', 'http://www.meetup.com/Bangalore-PHP-Users/feed/atom.html');$link->setAttribute('rel', 'alternate');
![Page 22: XML For PHP Developers](https://reader034.vdocuments.net/reader034/viewer/2022052315/554abe9ab4c90580698b4a2d/html5/thumbnails/22.jpg)
Create Other Nodes
$root->appendChild(new DOMElement('title', 'PHP User Group Meet'));
$root->appendChild(new DOMElement('id', 'http://example.com/my_unique_id.xml'));
$root->appendChild(new DOMElement('subtitle', 'Bangalore PHP Users'));
$root->appendChild(new DOMElement('updated', '2010-10-23T10:30:02Z'));
![Page 23: XML For PHP Developers](https://reader034.vdocuments.net/reader034/viewer/2022052315/554abe9ab4c90580698b4a2d/html5/thumbnails/23.jpg)
Create Entry Node$node = $doc->createElement('entry');$entry = $root->appendChild($node);
$node = $doc->createElement('link');$link = $entry->appendChild($node);$link->setAttribute('href', 'http://www.meetup.com/Bangalore-PHP-Users/calendar/15022884/atom.xml');$link->setAttribute('rel', 'self');
$node = $doc->createElement('link');$link = $entry->appendChild($node);$link->setAttribute('href', 'http://www.meetup.com/Bangalore-PHP-Users/calendar/15022884/atom.html');$link->setAttribute('rel', 'alternate');
![Page 24: XML For PHP Developers](https://reader034.vdocuments.net/reader034/viewer/2022052315/554abe9ab4c90580698b4a2d/html5/thumbnails/24.jpg)
Create Other Nodes In Entry
$entry->appendChild(new DOMElement('title', 'October 2010 Meet'));$entry->appendChild(new DOMElement('summary', 'Great meetup'));$entry->appendChild(new DOMElement('id', 'http://example.com/my_uique_feed_id.xml'));$entry->appendChild(new DOMElement('updated', '2010-10-27T10:30:02Z'));$node = $doc->createElement('author');$author = $entry->appendChild($node);$author->appendChild(new DOMElement('name', 'Sudheer Satyanarayana'));
![Page 25: XML For PHP Developers](https://reader034.vdocuments.net/reader034/viewer/2022052315/554abe9ab4c90580698b4a2d/html5/thumbnails/25.jpg)
Save As XML
$doc->formatOutput = TRUE;
print $doc->saveXML();
![Page 26: XML For PHP Developers](https://reader034.vdocuments.net/reader034/viewer/2022052315/554abe9ab4c90580698b4a2d/html5/thumbnails/26.jpg)
Scraping Websites
Grab HTMLTransform to XMLUse XPath to navigate the documentUse accessors to retrieve text content
![Page 27: XML For PHP Developers](https://reader034.vdocuments.net/reader034/viewer/2022052315/554abe9ab4c90580698b4a2d/html5/thumbnails/27.jpg)
Recipe 3
Retrieve list of members of Lok Sabha from Government website http://164.100.47.132/LssNew/Members/Alphabaticallist.aspx
![Page 28: XML For PHP Developers](https://reader034.vdocuments.net/reader034/viewer/2022052315/554abe9ab4c90580698b4a2d/html5/thumbnails/28.jpg)
<table id="ctl00_ContPlaceHolderMain_Alphabaticallist1_dg1"><tr><td>S.No.</td><td>Name of Member</td><td>Party Name</td><td>Constituency (State)</td></tr><tr><td></td><td><a>Aaroon Rasheed,Shri J.M.</a></td><td>Indian National Congress</td><td>Theni (Tamil Nadu )</td>
</tr><tr><td></td><td><a>Abdul Rahman,Shri</a></td><td>Dravida Munnetra Kazhagam</td><td>Vellore (Tamil Nadu )</td></tr></table>
![Page 29: XML For PHP Developers](https://reader034.vdocuments.net/reader034/viewer/2022052315/554abe9ab4c90580698b4a2d/html5/thumbnails/29.jpg)
Building Xpath ExpressionFind the table with the specified ID //table[@id='ctl00_ContPlaceHolderMain_Alphabaticallist1_dg1'] Select all table rows after position 1tr[position()>1] Select table cells with position 2td[position()=2] Select anchor elementaSelect all children that are text nodeschild::text()
![Page 30: XML For PHP Developers](https://reader034.vdocuments.net/reader034/viewer/2022052315/554abe9ab4c90580698b4a2d/html5/thumbnails/30.jpg)
Final Xpath Expression
//table[@id='ctl00_ContPlaceHolderMain_Alphabaticallist1_dg1']/tr[position()>1]/td[position()=2]/a/child::text()
![Page 31: XML For PHP Developers](https://reader034.vdocuments.net/reader034/viewer/2022052315/554abe9ab4c90580698b4a2d/html5/thumbnails/31.jpg)
PHP Script<?php$html = file_get_contents('http://164.100.47.132/LssNew/Members/Alphabaticallist.aspx');$doc = new DOMDocument();$doc->loadHtml($html);
$domxpath = new DOMXPath($doc);$xpath_expression = "//table[@id='ctl00_ContPlaceHolderMain_Alphabaticallist1_dg1']/tr[position()
>1]/td[position()=2]/a/child::text()";$result = $domxpath->evaluate($xpath_expression);
echo "Total number of memembers of Lok Sabha " . $result->length;
foreach ($result as $r) {
echo "\n" . $r->nodeValue;
}
![Page 32: XML For PHP Developers](https://reader034.vdocuments.net/reader034/viewer/2022052315/554abe9ab4c90580698b4a2d/html5/thumbnails/32.jpg)
QOTD
Retrieves quote of the day from Wikiquotes.orgCreates a feedAlso sends SMS to subscribers
http://qotd.techchorus.net http://labs.google.co.in/smschannels/channel/WikiQuoteOfTheDay
![Page 33: XML For PHP Developers](https://reader034.vdocuments.net/reader034/viewer/2022052315/554abe9ab4c90580698b4a2d/html5/thumbnails/33.jpg)
Where To Go From Here?
Start using XML right awayNow you know how to parse and create feedsLearn more XML technologies Read the specifications Scrape websitesImagination is your limitBuild the next big thing since sliced bread!
![Page 34: XML For PHP Developers](https://reader034.vdocuments.net/reader034/viewer/2022052315/554abe9ab4c90580698b4a2d/html5/thumbnails/34.jpg)
ResourcesDownload files shown in this presentation -
http://techchorus.net/downloads/xml-for-php-developers/xml-for-php-developers.tar.gz http://techchorus.net/downloads/xml-for-php-developers/xml-for-php-developers.zip
PHP Manual - http://in3.php.net/manual/en/refs.xml.phpTech Chorus - http://techchorus.netW3Schools - http://www.w3schools.com/xml/default.aspBook - Pro PHP And XML Web Services by Robert Richards. Review - http://techchorus.net/pro-php-xml-and-web-services-book-reviewMore useful links - http://xml.farsquare.com/
![Page 35: XML For PHP Developers](https://reader034.vdocuments.net/reader034/viewer/2022052315/554abe9ab4c90580698b4a2d/html5/thumbnails/35.jpg)
Questions?
![Page 36: XML For PHP Developers](https://reader034.vdocuments.net/reader034/viewer/2022052315/554abe9ab4c90580698b4a2d/html5/thumbnails/36.jpg)
Thank You
The slides will be available at SlideSharehttp://www.slideshare.net/bngsudheer
My Twitter handle: @bngsudheer
Blog: http://techchorus.net
Business: http://binaryvibes.co.in
E-mail: sudheer @ above business URL
![Page 37: XML For PHP Developers](https://reader034.vdocuments.net/reader034/viewer/2022052315/554abe9ab4c90580698b4a2d/html5/thumbnails/37.jpg)
License
XML For PHP Developers by Sudheer Satyanarayana is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 2.5 India License . Based on a work at techchorus.net . Permissions beyond the scope of this license may be available at http://techchorus.net .