SlideShare una empresa de Scribd logo
1 de 37
Descargar para leer sin conexión
XML For PHP Developers
by 
Sudheer Satyanarayana
http://techchorus.net
Bangalore PHP User Group Meetup
30 October 2010
Agenda
Quick introduction to XML basics
Recipe 1 - parsing RSS feed
Recipe 2 - creating Atom feed
Recipe 3 - scraping information from websites using
XML
Question and answer
At the end of the session, you will be able to start using
XML with PHP.
XML Basics
XML document has a tree structure
XML documents can be validated using an XML schema
All major programming languages support reading and
writing XML documents
Hundreds of technologies are built on top of XML
XHTML is one of the XML markup languages
XML Parsers In PHP
simplexml
DOM
SAX
XMLReader
XMLWriter
Which Parser To Use
simplexml - really simple XML documents
DOM - heavy lifting
SAX - large XML documents
XMLReader - large XML documents
Don't use:
string manipulation to create XML documents
regular expressions to parse XML documents
Recipe 1
simplexml usage
Programmatically retrieve weather information in your
location
Using Yahoo! Weather
parse RSS feed
RSS - Really Simple Syndication
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>
Retrieving weather info from Yahoo!
http://weather.yahooapis.com/forecastrss?w=2442047&u=c
w = location, WOEID
u = degrees units (Fahrenheit or Celsius)
http://weather.yahooapis.com/forecastrss?w=2295420&u=c
<?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>
Visualization
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;
Output
Current Conditions:
Haze, 22 C
Forecast:
Tue - Partly Cloudy. High: 26 Low: 19
Wed - Isolated Thunderstorms. High: 27 Low: 19
Full Forecast at Yahoo! Weather
(provided by The Weather Channel)
Atom
Atom - key takeaways
Disagreements in RSS community
Atom entry document
Atom feed document
Atom Publishing Protocol
Use Atom if you are a feed publisher
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>
Atom Feed Structure
Atom Structure
<link href="..." rel="self">
<link href="..." rel="alternate">
<title>...</title>
<id>...</id>
<summary>...</summary>
<update>...</updated>
<author><name>...</name></author>
Recipe 2
Generate an Atom feed
PHP DOM
Create An Atom Feed Document
<?php
$doc = DOMDocument::loadXML('<feed/>');
$root = $doc->documentElement;
$root->setAttribute('xmlns','http://www.w3.org/2005/Atom');
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');
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'));
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');
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'));
Save As XML
$doc->formatOutput = TRUE;
print $doc->saveXML();
Scraping Websites
Grab HTML
Transform to XML
Use XPath to navigate the document
Use accessors to retrieve text content
Recipe 3
Retrieve list of members of Lok Sabha from Government
website
http://164.100.47.132/LssNew/Members/Alphabaticallist.aspx
<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>
Building Xpath Expression
Find the table with the specified ID
//table[@id='ctl00_ContPlaceHolderMain_Alphabaticallist1_dg1']
Select all table rows after position 1
tr[position()>1]
Select table cells with position 2
td[position()=2]
Select anchor element
a
Select all children that are text nodes
child::text()
Final Xpath Expression
//table[@id='ctl00_ContPlaceHolderMain_Alphabaticallist1_dg1']
/tr[position()>1]
/td[position()=2]
/a
/child::text()
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;
}
QOTD
Retrieves quote of the day from Wikiquotes.org
Creates a feed
Also sends SMS to subscribers
http://qotd.techchorus.net
http://labs.google.co.in/smschannels/channel/WikiQuoteOfTheDay
Where To Go From Here?
Start using XML right away
Now you know how to parse and create feeds
Learn more XML technologies
Read the specifications
Scrape websites
Imagination is your limit
Build the next big thing since sliced bread!
Resources
Download 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.php
Tech Chorus - http://techchorus.net
W3Schools - http://www.w3schools.com/xml/default.asp
Book - Pro PHP And XML Web Services by Robert
Richards. Review - http://techchorus.net/pro-php-xml-
and-web-services-book-review
More useful links - http://xml.farsquare.com/
Questions?
Thank You
The slides will be available at SlideShare
http://www.slideshare.net/bngsudheer
My Twitter handle: @bngsudheer
Blog: http://techchorus.net
Business: http://binaryvibes.co.in
E-mail: sudheer @ above business URL
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 .

Más contenido relacionado

Similar a XML For PHP Developers

Similar a XML For PHP Developers (20)

Web Information Systems XML
Web Information Systems XMLWeb Information Systems XML
Web Information Systems XML
 
Web Developement Workshop (Oct 2009 -Day 1)
Web Developement Workshop (Oct 2009 -Day 1)Web Developement Workshop (Oct 2009 -Day 1)
Web Developement Workshop (Oct 2009 -Day 1)
 
Java XML Parsing
Java XML ParsingJava XML Parsing
Java XML Parsing
 
6 311 W
6 311 W6 311 W
6 311 W
 
6 311 W
6 311 W6 311 W
6 311 W
 
test
testtest
test
 
Advanced Web Programming Chapter 12
Advanced Web Programming Chapter 12Advanced Web Programming Chapter 12
Advanced Web Programming Chapter 12
 
Processing XML with Java
Processing XML with JavaProcessing XML with Java
Processing XML with Java
 
Boost Your Environment With XMLDB - UKOUG 2008 - Marco Gralike
Boost Your Environment With XMLDB - UKOUG 2008 - Marco GralikeBoost Your Environment With XMLDB - UKOUG 2008 - Marco Gralike
Boost Your Environment With XMLDB - UKOUG 2008 - Marco Gralike
 
WEB PROGRAMMING
WEB PROGRAMMINGWEB PROGRAMMING
WEB PROGRAMMING
 
Fluentd and Embulk Game Server 4
Fluentd and Embulk Game Server 4Fluentd and Embulk Game Server 4
Fluentd and Embulk Game Server 4
 
Tml for Laravel
Tml for LaravelTml for Laravel
Tml for Laravel
 
How web applications work
How web applications workHow web applications work
How web applications work
 
Basics of XML
Basics of XMLBasics of XML
Basics of XML
 
Xml presentation
Xml presentationXml presentation
Xml presentation
 
Microformats nishikant-taksande
Microformats nishikant-taksandeMicroformats nishikant-taksande
Microformats nishikant-taksande
 
CrashCourse: XML technologies
CrashCourse: XML technologiesCrashCourse: XML technologies
CrashCourse: XML technologies
 
DIWE - Coding HTML for Basic Web Designing
DIWE - Coding HTML for Basic Web DesigningDIWE - Coding HTML for Basic Web Designing
DIWE - Coding HTML for Basic Web Designing
 
eXtensible Markup Language (XML)
eXtensible Markup Language (XML)eXtensible Markup Language (XML)
eXtensible Markup Language (XML)
 
Deep dive formatting
Deep dive formattingDeep dive formatting
Deep dive formatting
 

Más de Sudheer Satyanarayana

How To Build A Bulk Email Sending Application In PHP
How To Build A Bulk Email Sending Application In PHPHow To Build A Bulk Email Sending Application In PHP
How To Build A Bulk Email Sending Application In PHPSudheer Satyanarayana
 
Introduction to-bizsense-bpug-29-05-2010
Introduction to-bizsense-bpug-29-05-2010Introduction to-bizsense-bpug-29-05-2010
Introduction to-bizsense-bpug-29-05-2010Sudheer Satyanarayana
 
Building Restful Applications Using Php
Building Restful Applications Using PhpBuilding Restful Applications Using Php
Building Restful Applications Using PhpSudheer Satyanarayana
 
Mysql Naming Conventions Bangalore Mysql User Group Oct 11 09
Mysql Naming Conventions Bangalore Mysql User Group Oct 11 09Mysql Naming Conventions Bangalore Mysql User Group Oct 11 09
Mysql Naming Conventions Bangalore Mysql User Group Oct 11 09Sudheer Satyanarayana
 

Más de Sudheer Satyanarayana (6)

How To Build A Bulk Email Sending Application In PHP
How To Build A Bulk Email Sending Application In PHPHow To Build A Bulk Email Sending Application In PHP
How To Build A Bulk Email Sending Application In PHP
 
Introduction to-bizsense-bpug-29-05-2010
Introduction to-bizsense-bpug-29-05-2010Introduction to-bizsense-bpug-29-05-2010
Introduction to-bizsense-bpug-29-05-2010
 
Bare acl
Bare aclBare acl
Bare acl
 
Framework Shootout ZF
Framework Shootout ZFFramework Shootout ZF
Framework Shootout ZF
 
Building Restful Applications Using Php
Building Restful Applications Using PhpBuilding Restful Applications Using Php
Building Restful Applications Using Php
 
Mysql Naming Conventions Bangalore Mysql User Group Oct 11 09
Mysql Naming Conventions Bangalore Mysql User Group Oct 11 09Mysql Naming Conventions Bangalore Mysql User Group Oct 11 09
Mysql Naming Conventions Bangalore Mysql User Group Oct 11 09
 

XML For PHP Developers

  • 1. XML For PHP Developers by  Sudheer Satyanarayana http://techchorus.net Bangalore PHP User Group Meetup 30 October 2010
  • 2. Agenda Quick introduction to XML basics Recipe 1 - parsing RSS feed Recipe 2 - creating Atom feed Recipe 3 - scraping information from websites using XML Question and answer At the end of the session, you will be able to start using XML with PHP.
  • 3. XML Basics XML document has a tree structure XML documents can be validated using an XML schema All major programming languages support reading and writing XML documents Hundreds of technologies are built on top of XML XHTML is one of the XML markup languages
  • 4. XML Parsers In PHP simplexml DOM SAX XMLReader XMLWriter
  • 5. Which Parser To Use simplexml - really simple XML documents DOM - heavy lifting SAX - large XML documents XMLReader - large XML documents Don't use: string manipulation to create XML documents regular expressions to parse XML documents
  • 6. Recipe 1 simplexml usage Programmatically retrieve weather information in your location Using Yahoo! Weather parse RSS feed
  • 7. RSS - Really Simple Syndication
  • 8. 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>
  • 9. Retrieving weather info from Yahoo! http://weather.yahooapis.com/forecastrss?w=2442047&u=c w = location, WOEID u = degrees units (Fahrenheit or Celsius) http://weather.yahooapis.com/forecastrss?w=2295420&u=c
  • 10. <?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>
  • 12. 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;
  • 13. Output Current Conditions: Haze, 22 C Forecast: Tue - Partly Cloudy. High: 26 Low: 19 Wed - Isolated Thunderstorms. High: 27 Low: 19 Full Forecast at Yahoo! Weather (provided by The Weather Channel)
  • 14. Atom
  • 15. Atom - key takeaways Disagreements in RSS community Atom entry document Atom feed document Atom Publishing Protocol Use Atom if you are a feed publisher
  • 16. 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>
  • 18. Atom Structure <link href="..." rel="self"> <link href="..." rel="alternate"> <title>...</title> <id>...</id> <summary>...</summary> <update>...</updated> <author><name>...</name></author>
  • 19. Recipe 2 Generate an Atom feed PHP DOM
  • 20. Create An Atom Feed Document <?php $doc = DOMDocument::loadXML('<feed/>'); $root = $doc->documentElement; $root->setAttribute('xmlns','http://www.w3.org/2005/Atom');
  • 21. 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');
  • 22. 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'));
  • 23. 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');
  • 24. 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'));
  • 25. Save As XML $doc->formatOutput = TRUE; print $doc->saveXML();
  • 26. Scraping Websites Grab HTML Transform to XML Use XPath to navigate the document Use accessors to retrieve text content
  • 27. Recipe 3 Retrieve list of members of Lok Sabha from Government website http://164.100.47.132/LssNew/Members/Alphabaticallist.aspx
  • 28. <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>
  • 29. Building Xpath Expression Find the table with the specified ID //table[@id='ctl00_ContPlaceHolderMain_Alphabaticallist1_dg1'] Select all table rows after position 1 tr[position()>1] Select table cells with position 2 td[position()=2] Select anchor element a Select all children that are text nodes child::text()
  • 31. 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; }
  • 32. QOTD Retrieves quote of the day from Wikiquotes.org Creates a feed Also sends SMS to subscribers http://qotd.techchorus.net http://labs.google.co.in/smschannels/channel/WikiQuoteOfTheDay
  • 33. Where To Go From Here? Start using XML right away Now you know how to parse and create feeds Learn more XML technologies Read the specifications Scrape websites Imagination is your limit Build the next big thing since sliced bread!
  • 34. Resources Download 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.php Tech Chorus - http://techchorus.net W3Schools - http://www.w3schools.com/xml/default.asp Book - Pro PHP And XML Web Services by Robert Richards. Review - http://techchorus.net/pro-php-xml- and-web-services-book-review More useful links - http://xml.farsquare.com/
  • 36. Thank You The slides will be available at SlideShare http://www.slideshare.net/bngsudheer My Twitter handle: @bngsudheer Blog: http://techchorus.net Business: http://binaryvibes.co.in E-mail: sudheer @ above business URL
  • 37. 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 .