I have opinions

PHP class: SimpleXML

One thing I have found is the lack of really simple XML classes—that’s not to say there aren’t any good ones out there, I have used several really cracking ones. For my new version of my personal site (yes, this site is planned for a revamp) I wanted to integrate my Twitter feed somewhere. I looked and looked for a really simple XML class but was unable to find one. So I made one. And here is the result:

The code

_url = $url;
      $this->_xml_parser = xml_parser_create();
      xml_set_element_handler($this->_xml_parser,
         array($this, 'startTag'),
         array($this, 'endTag'));
      xml_set_character_data_handler($this->_xml_parser,
         array($this, 'contents'));
   }
 
   /**
    * Set a wanted attribute
    * @access public
    * @param mixed $want
    */ 
   public function setWant($want) {
      if (is_array($want)) {
         foreach ($want as $value) {
            $this->_want[] = $value;
         }
      }
      else {
         $this->_want[] = $want;
      }
      return $this;
   }
 
   /**
    * Set the ignore
    * @access private
    * @param boolean $ignore
    */
   private function setIgnore($ignore) {
      $this->_ignore = (bool)$ignore;
      return $this;
   }
 
   /**
    * Returns the ignore variable
    * @access private
    * @return boolean
    */
   private function getIgnore() {
      return $this->_ignore;
   }
 
   /**
    * Return the want array
    * @access private
    * @return array
    */
   private function getWant() {
      return (array)$this->_want;
   }
 
   /**
    * Parses the XML input
    * @access public
    */
   public function parse() {
      $fp = fopen($this->_url, "r"); 
      $data = fread($fp, 80000);
 
      if(!(xml_parse($this->_xml_parser, $data, feof($fp)))){ 
          die("Error on line " . xml_get_current_line_number($this->_xml_parser)); 
      }
   }
 
   /**
    * Set whether to output this attribute
    * @access private
    * @param object $parser
    * @param string $data
    */
   private function startTag($parser, $data){ 
      $this->_ignore = in_array(strtolower($data), $this->getWant())
          ? true
          : false;
   } 
 
   /**
    * This function does nothing, but is needed to stop parse errors
    * @access private
    * @param object $parser
    * @param string $data
    */
   private function endTag($parser, $data){ 
      // Do nothing
   }
 
   /**
    * Echo out the contents of the atttribute
    * @access private
    * @param object $parser
    * @param string $data
    */
   private function contents($parser, $data){ 
      $data = trim($data);
      if ($this->_ignore && !empty($data)) {
         if (count($this->_return[(count($this->_return) - 1)]) == count($this->_want)) {
            $this->_return[count($this->_return)][] = $data;
         }
         else {
            $this->_return[(count($this->_return) == 0 ? 0 : count($this->_return) - 1)][] = $data;
         }
      }
   }
 
   /**
    * Return the output array
    * @access public
    * @return array
    */
   public function getReturnArray() {
      return $this->_return;
   }
}

And you use it like this

$myXmlParser = new myXmlParser('http://twitter.com/statuses/user_timeline/chrisjhill.xml');
$myXmlParser->setWant(array('text', 'created_at'))->parse();
echo '';
   print_r($myXmlParser->getReturnArray());
echo '';

Which will produce the following result:

Array
(
    [0] => Array
        (
            [0] => Tue Mar 25 16:24:41 +0000 2008
            [1] => Just got back from a challenging run, don't feel too tired though...
        )
 
    [1] => Array
        (
            [0] => Mon Feb 25 18:14:21 +0000 2008
            [1] => I'm fixing my XML parser class. It's better than I remember, I might consider posting it on my blog!
        )
 
    [2] => Array
        (
            [0] => Sat Jan 19 21:44:14 +0000 2008
            [1] => I cannot believe it. My se7ev DVD has had its end corrupted. 1 hour 40 of my life wasted...
        )
 
)

Now thats some nice output

I love working with arrays, the more complex they are the better. This one is quite simple though, it adds each feed into a array index which has 2 children; the date posted and the content. Simple.

A little information on using it

The class works by first supplying a URL to the XML source in the constructor. You then set what fields you want to save, and discard the rest. This saves memory without having to store data you will not use. Call the parse() function and it will work its magic. And yes, the class can make use of method chaining. Enjoy.