【php开源代码栏目提醒】:文章导读:在新的一年中,各位网友都进入紧张的学习或是工作阶段。网学会员整理了php开源代码-class.Atom.php的相关内容供大家参考,祝大家在新的一年里工作和学习顺利!
<?php
class Atom {
/*
* Useage Example:
*
* $xml = "<?php xml version="1.0"......
*
* $atom = new atom( $xml );
*
* // print feed title
* echo $atom->feed['title'];
*
* // print the title of each entry
* foreach ($atom->entries as $entry ) {
* echo $entry[title];
* }
*
*/
var $parser;
var $current_item = array(); // item currently being parsed
var $entries = array(); // collection of parsed items
var $feed = array(); // hash of channel fields
var $parent_field = array('RDF');
var $author = array();
var $contributor = array();
var $attributes = array();
var $current_field = '';
var $current_namespace = false;
var $contentmode = '';
var $xmlcontent = '';
var $ERROR = "";
/*======================================================================*\
Function: Atom
Purpose: Constructor, sets up XML parser,parses source,
and populates object..
Input: String containing the Atom feed to be parsed
\*======================================================================*/
function Atom ($source) {
# if PHP xml isn't compiled in, die
#
if (!function_exists('xml_parser_create')) {
$this->error( "Failed to load PHP's XML Extension. " .
"http://www.php.net/manual/en/ref.xml.php",
E_USER_ERROR );
}
$parser = @xml_parser_create();
if (!is_resource($parser))
{
$this->error( "Failed to create an instance of
PHP's XML parser. " .
"http://www.php.net/manual/en/ref.xml.php",
E_USER_ERROR );
}
$this->parser = $parser;
# pass in parser, and a reference to this object
# setup handlers
#
xml_set_object( $this->parser, $this );
xml_set_element_handler($this->parser, 'start_element', 'end_element');
xml_set_character_data_handler( $this->parser, 'cdata' );
$status = xml_parse( $this->parser, $source );
if (! $status ) {
$errorcode = xml_get_error_code( $this->parser );
if ( $errorcode != XML_ERROR_NONE ) {
$xml_error = xml_error_string( $errorcode );
$error_line = xml_get_current_line_number($this->parser);
$error_col = xml_get_current_column_number($this->parser);
$errormsg = "$xml_error at line $error_line, column $error_col";
$this->error( $errormsg );
}
}
xml_parser_free( $this->parser );
}
function start_element ($p, $element, &$attrs) {
$element = strtolower( $element );
# check for a namespace, and split if found
#
$namespace = false;
if ( strpos( $element, ':' ) ) {
list($namespace, $element) = split( ':', $element, 2);
}
$this->current_field = $element;
$this->attributes = $attrs;
if ( $namespace ) {
$this->current_namespace = $namespace;
}
if ( $element == 'feed' ) {
array_unshift( $this->parent_field, 'feed' );
}
elseif ( $element == 'items' ) {
array_unshift( $this->parent_field, 'items' );
}
elseif ( $element == 'entry' ) {
array_unshift( $this->parent_field, 'entry' );
}
elseif ( $element == 'author' ) {
array_unshift( $this->parent_field, 'author' );
}
elseif ( $element == 'contributor' ) {
array_unshift( $this->parent_field, 'contributor' );
}
elseif ( $element == 'content') {
$this->contentmode = $attrs['MODE'];
}
elseif( $element == 'link') {
if($this->parent_field[0] == 'entry') {
$this->append($this->current_item[$this->current_field],$attrs['HREF']);
} elseif($this->parent_field[0] == 'feed') {
$this->append($this->feed[$this->current_field],$attrs['HREF']);
}
}
}
function end_element ($p, $element) {
$element = strtolower($element);
if ( $element == 'entry' ) {
$this->entries[] = $this->current_item;
$this->current_item = array();
array_shift( $this->parent_field );
}
elseif ( $element == 'feed' or $element == 'items' or
$element == 'author' or $element == 'contributor') {
array_shift( $this->parent_field );
}
$this->current_field = '';
$this->current_namespace = false;
}
function cdata ($p, $text) {
# skip item, channel, items first time we see them
#
if ( $this->parent_field[0] == $this->current_field or
! $this->current_field ) {
return;
}
elseif ( $this->parent_field[0] == 'feed') {
if ( $this->current_namespace ) {
$this->append(
$this->feed[ $this->current_namespace ][ $this->current_field ],
$text);
}
else {
$this->append($this->feed[ $this->current_field ], $text);
}
}
elseif ( $this->parent_field[0] == 'entry' ) {
if ( $this->current_names