2007 barcelona drupalcon: drupal and simplexml

32
Drupal and SimpleXML

Upload: james-walker

Post on 12-Nov-2014

10.052 views

Category:

Business


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: 2007 Barcelona Drupalcon: Drupal and SimpleXML

Drupal and SimpleXML

Page 2: 2007 Barcelona Drupalcon: Drupal and SimpleXML

XML parsing

• Very well defined, well known standards

• Excellent support ...

• ... in most other languages

• Long-standing PHP weakness (up to version 4)

Page 3: 2007 Barcelona Drupalcon: Drupal and SimpleXML

two main models

Page 4: 2007 Barcelona Drupalcon: Drupal and SimpleXML

SAX

Page 5: 2007 Barcelona Drupalcon: Drupal and SimpleXML

Simple API for XML

• Stream - based xml parsing

• Event - driven API

• Uni-directional

Page 6: 2007 Barcelona Drupalcon: Drupal and SimpleXML

DOM

Page 7: 2007 Barcelona Drupalcon: Drupal and SimpleXML

Document Object Model

• Full object representation of an XML document

• Supports writing documents

• Same system used by browsers, javascript, etc (i.e. “known quantity”)

Page 8: 2007 Barcelona Drupalcon: Drupal and SimpleXML

Other key concepts

• Namespaces: Allow extending existing XML documents

• XPath: selector syntax for addressing parts of a document

• XSLT: “template” language for transforming XML documents into other formats

Page 9: 2007 Barcelona Drupalcon: Drupal and SimpleXML

DOM in PHP

• http://php.net/domxml

• Traditionally required external libraries and additional PHP compile flags

• Has been removed from PHP core in PHP5(available as a PECL module)

• not really an option for Drupal core

Page 10: 2007 Barcelona Drupalcon: Drupal and SimpleXML

SAX in PHP

• Available in PHP 4 & PHP 5 ( --with-xml )

• Closest “built-in” support

• (hence what we use in Drupal)

• http://php.net/xml

Page 11: 2007 Barcelona Drupalcon: Drupal and SimpleXML

SAX example

Page 12: 2007 Barcelona Drupalcon: Drupal and SimpleXML

SAX example (cont.)

Page 13: 2007 Barcelona Drupalcon: Drupal and SimpleXML

SAX example (cont)

Page 14: 2007 Barcelona Drupalcon: Drupal and SimpleXML

SAX example (cont)

Page 15: 2007 Barcelona Drupalcon: Drupal and SimpleXML

SAX drawbacks

• Awkward maintain state across events

• Requires prior knowledge of the document

• Maybe not so simple (lots to code)

Page 16: 2007 Barcelona Drupalcon: Drupal and SimpleXML

Enter SimpleXML

Page 17: 2007 Barcelona Drupalcon: Drupal and SimpleXML

SimpleXML

• DOM-based processing - including xpath

• Included in PHP5

• And, um, it’s simple

Page 18: 2007 Barcelona Drupalcon: Drupal and SimpleXML

SimpleXML example

Page 19: 2007 Barcelona Drupalcon: Drupal and SimpleXML

Example file

Page 20: 2007 Barcelona Drupalcon: Drupal and SimpleXML

SimpleXML example

Page 21: 2007 Barcelona Drupalcon: Drupal and SimpleXML

XPath example

Page 22: 2007 Barcelona Drupalcon: Drupal and SimpleXML

SimpleXML+

drupal_http_request() ftw

Page 23: 2007 Barcelona Drupalcon: Drupal and SimpleXML

simple webservices

Page 24: 2007 Barcelona Drupalcon: Drupal and SimpleXML

When SimpleXML isn’t simple

Page 25: 2007 Barcelona Drupalcon: Drupal and SimpleXML

namespaces

Page 26: 2007 Barcelona Drupalcon: Drupal and SimpleXML

Example file

Page 27: 2007 Barcelona Drupalcon: Drupal and SimpleXML

Namespaces example

Page 28: 2007 Barcelona Drupalcon: Drupal and SimpleXML

maybe it’s not so bad

Page 29: 2007 Barcelona Drupalcon: Drupal and SimpleXML

XML Writing

Page 30: 2007 Barcelona Drupalcon: Drupal and SimpleXML

XML Writing example

Page 31: 2007 Barcelona Drupalcon: Drupal and SimpleXML

Advantages for Drupal

• Shorter, cleaner code (think cleaner aggregator.module, etc)

• Manipulatable DOM structure (think hook_xml_alter)

• XML Writing (think node_feed, etc)

Page 32: 2007 Barcelona Drupalcon: Drupal and SimpleXML

Thanks, PHP5