Drupal and SimpleXML
XML parsing

• Very well defined, well known standards
• Excellent support ...
• ... in most other languages
• Long-standin...
two main models
SAX
Simple API for XML

• Stream - based xml parsing
• Event - driven API
• Uni-directional
DOM
Document Object Model

• Full object representation of an XML
  document
• Supports writing documents
• Same system used b...
Other key concepts

• Namespaces: Allow extending existing XML
  documents
• XPath: selector syntax for addressing parts
 ...
DOM in PHP
• http://php.net/domxml
• Traditionally required external libraries and
  additional PHP compile flags
• Has bee...
SAX in PHP

• Available in PHP 4 & PHP 5 ( --with-xml )
• Closest “built-in” support
 • (hence what we use in Drupal)
• ht...
SAX example
SAX example (cont.)
SAX example (cont)
SAX example (cont)
SAX drawbacks

• Awkward maintain state across events
• Requires prior knowledge of the document
• Maybe not so simple (lo...
Enter SimpleXML
SimpleXML

• DOM-based processing - including xpath
• Included in PHP5
• And, um, it’s simple
SimpleXML example
Example file
SimpleXML example
XPath example
SimpleXML
           +
drupal_http_request() ftw
simple webservices
When SimpleXML isn’t
       simple
namespaces
Example file
Namespaces example
maybe it’s not so bad
XML Writing
XML Writing example
Advantages for Drupal

• Shorter, cleaner code (think cleaner
  aggregator.module, etc)
• Manipulatable DOM structure (thi...
Thanks, PHP5
Upcoming SlideShare
Loading in …5
×

2007 Barcelona Drupalcon: Drupal and SimpleXML

7,386 views
7,283 views

Published on

Published in: Business, Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
7,386
On SlideShare
0
From Embeds
0
Number of Embeds
47
Actions
Shares
0
Downloads
152
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

2007 Barcelona Drupalcon: Drupal and SimpleXML

  1. 1. Drupal and SimpleXML
  2. 2. XML parsing • Very well defined, well known standards • Excellent support ... • ... in most other languages • Long-standing PHP weakness (up to version 4)
  3. 3. two main models
  4. 4. SAX
  5. 5. Simple API for XML • Stream - based xml parsing • Event - driven API • Uni-directional
  6. 6. DOM
  7. 7. Document Object Model • Full object representation of an XML document • Supports writing documents • Same system used by browsers, javascript, etc (i.e. “known quantity”)
  8. 8. 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
  9. 9. 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
  10. 10. 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
  11. 11. SAX example
  12. 12. SAX example (cont.)
  13. 13. SAX example (cont)
  14. 14. SAX example (cont)
  15. 15. SAX drawbacks • Awkward maintain state across events • Requires prior knowledge of the document • Maybe not so simple (lots to code)
  16. 16. Enter SimpleXML
  17. 17. SimpleXML • DOM-based processing - including xpath • Included in PHP5 • And, um, it’s simple
  18. 18. SimpleXML example
  19. 19. Example file
  20. 20. SimpleXML example
  21. 21. XPath example
  22. 22. SimpleXML + drupal_http_request() ftw
  23. 23. simple webservices
  24. 24. When SimpleXML isn’t simple
  25. 25. namespaces
  26. 26. Example file
  27. 27. Namespaces example
  28. 28. maybe it’s not so bad
  29. 29. XML Writing
  30. 30. XML Writing example
  31. 31. Advantages for Drupal • Shorter, cleaner code (think cleaner aggregator.module, etc) • Manipulatable DOM structure (think hook_xml_alter) • XML Writing (think node_feed, etc)
  32. 32. Thanks, PHP5

×