Quantcast
Channel: LampJunkie.com - Everything related to Linux, PHP, MySQL, Apache, AJAX, Symfony and more
Viewing all articles
Browse latest Browse all 10

Wishing that PHP had something like JAXB

$
0
0

Update: Read about Hitch, my JAXB-like XML annotation mapper for PHP 5.3: Introducing Hitch

Lately I have been doing alot of Java development especially in regards to web service development and consumption. One of the tools that I have relied on heavily is the JAXB project to consume and create XML feeds. This has been a godsend and made my life easy when parsing and creating XML. By just using a small amount of code and annotations parsing XML is a breeze.

After using JAXB for awhile, it got me thinking….why doesn’t PHP have something like this…how hard would it be to create? On the PHP side of things I have been in love with SimpleXML since I first started using it a number of years ago. Just like the name implies, it is relatively simple. But, could something be developed that would be even simpler like JAXB?

The thing that really makes parsing XML with JAXB easy is the ability to use annotations to map classes and properties to XML nodes. Of course PHP doesn’t have built-in support for annotations, but this can be achieved by using the Extended Reflection API or using a project built on top of that such as addendum. It would also be possible to create a PHP extension to handle this in a much more efficient manner.

This is how I envision being able to parse (unmarshal) XML to a PHP object graph:

Given the following sample XML:

  1.  
  2. <result>
  3.  <ads>
  4.   <ad url="http://ad1-url">
  5.    <title>My Title</title>
  6.    <description>Some Description</description>
  7.   </ad>
  8.   <ad url="http://ad2-url">
  9.    <title>My Title</title>
  10.    <description>Some Description</description>
  11.   </ad>
  12.  </ads>
  13. </result>

Convert to an object graph like this:

  1.  
  2. /**
  3.  * @XmlRoot(name="result")
  4.  */
  5. class Result {
  6.  
  7.  /**
  8.   * @XmlCollection(name="ads")
  9.   * @var array
  10.   */
  11.  private $ads;
  12.  
  13.  // getters/setters
  14. }
  15.  
  16. /**
  17.  * @XmlElement(name="ad")
  18.  */
  19. class Ad {
  20.  
  21.  /**
  22.   * @XmlAttribute(name="url")
  23.   * @var string
  24.   */
  25.  private $url;
  26.  
  27.  /**
  28.   * @XmlElement(name="title")
  29.   * @var string
  30.   */
  31.  private $title;
  32.  
  33.  /**
  34.   * @XmlElement(name="description")
  35.   * @var string
  36.   */
  37.  private $description;
  38.  
  39.  // getters/setters
  40. }
  41.  
  42. // grab an XML feed
  43. $xml = file_get_contents('some-xml-from-somewhere.xml');
  44.  
  45. // create our unmarshaller and tell it where to find classes to unmarshal to
  46. $unmarhaller = new SimplerXmlUnmarshaller("/package/location");
  47.  
  48. // unmarshal to an object graph
  49. $result = $unmarshaller->unmarshal($xml);
  50.  
  51. // loop through objects
  52. foreach($result->getAds() as $ad){
  53.  echo $ad->getUrl() . '<br />' . $ad->getTitle() . '<br />' . $ad->getDescription() . '<br />';
  54. }

Hopefully in the near future I will get time to develop a proof of concept to see if this works and take it from there…


Viewing all articles
Browse latest Browse all 10

Trending Articles