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
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
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
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
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 .