XML PHP

:
, 21:56
1
2687

xml php

XML RSS (Rich Site Summary), (, ) , , XML- .
xml php RSS 0.91 Netscape "My Netscape Network", RSS XML- , , , "", "" "". , , RSS- , HTML? php -, PHP XML- . PHP expat , , Apache 1.3.9 . XML PHP, - with-xml PHP .
xml , RSS- , , HTML. ³ , XML PHP, - , .

, - , :

class xItem {
var $xTitle;
var $xLink;
var $xDescription;
}

' :
$ sTitle = "";
$ sLink = "";
$ sDescription = "";
$ arItems = array();
$ itemCount = 0;

:
function startElement($ parser, $ name, $attrs) {
global $ curTag;
$ curTag .= "^$ name";
}
function endElement($ parser, $ name) {
global $ curTag;
$caret_pos = strrpos($ curTag,^);
$ curTag = substr($ curTag,0,$caret_pos);
}

XML PHP, , , :

  • ;
  • ;
  • .

: ($ curTag) , , ^. , XML-:

$ curTag :
^ RSS^CHANNEL^ITEM

, - , $ curTag, . characterData. :
function characterData($ parser, $data) {
global $ curTag; //
global $ sTitle, $ sLink, $ sDescription;
$titleKey = "^ RSS^CHANNEL^TITLE";
$linkKey = "^ RSS^CHANNEL^LINK";
$descKey = "^ RSS^CHANNEL^DESCRIPTION";
if ($ curTag == $titleKey) {
$ sTitle = $data;
} elseif ($ curTag == $linkKey) {
$ sLink = $data;
} elseif ($ curTag == $descKey) {
$ sDescription = $data;
}
//
global $ arItems, $ itemCount;
$itemTitleKey = "^ RSS^CHANNEL^ITEM^TITLE";
$itemLinkKey = "^ RSS^CHANNEL^ITEM^LINK";
$itemDescKey = "^ RSS^CHANNEL^ITEM^DESCRIPTION";
if ($ curTag == $itemTitleKey) {
// xItem
$ arItems[$ itemCount] = new xItem();
//
$ arItems[$ itemCount]->xTitle = $data;
} elseif ($ curTag == $itemLinkKey) {
$ arItems[$ itemCount]->xLink = $data;
} elseif ($ curTag == $itemDescKey) {
$ arItems[$ itemCount]->xDescription = $data;
//
$ itemCount++;
}
}

xml html , $ curTag , , . , , . , xItem, $ arItems RSS- .

, , PHP ' XML- :
//
$xml_ parser = xml_ parser_create();
xml_set_element_handler($xml_ parser, "startElement", "endElement");
xml_set_character_data_handler($xml_ parser, " characterData");
if (!($fp = fopen($uFile,"r"))) {
die (" RSS");
}
while ($data = fread($fp, 4096)) {
if (!xml_parse($xml_ parser, $data, feof($fp))) {
die(sprintf("XML : %s %d", xml_error_string(xml_get_error_code($xml_ parser)), xml_get_current_line_number($xml_ parser)));
}
}
xml_ parser_free($xml_ parser);

, "xml_" - XML- PHP. , , , - , RSS ( $uFile ​​ RSS- ) (xml_parse).

, , HTML:

xml php , , .
, XML . XML-, ( RSS) - , 㳿.

:
class xItem {
var $xTitle;
var $xLink;
var $xDescription;
}

// general vars
$ sTitle = "";
$ sLink = "";
$ sDescription = "";
$ arItems = array();
$ itemCount = 0;

// ********* Start User-Defined Vars ************
// rss url goes here
$uFile = "http://www.wirelessdevnet.com/wirelessnews/ rss/dailynews. rss";
// descriptions (true or false) goes here
$bDesc = true;
// font goes here
$uFont = "Verdana, Arial, Helvetica, sans-serif";
$uFontSize = "2";
// ********* End User-Defined Vars **************

function startElement($ parser, $ name, $attrs) {
global $ curTag;

$ curTag .= "^$ name";

}

function endElement($ parser, $ name) {
global $ curTag;

$caret_pos = strrpos($ curTag,^);

$ curTag = substr($ curTag,0,$caret_pos);

}

function characterData($ parser, $data) { global $ curTag; // get the Channel information first
global $ sTitle, $ sLink, $ sDescription;
$titleKey = "^ RSS^CHANNEL^TITLE";
$linkKey = "^ RSS^CHANNEL^LINK";
$descKey = "^ RSS^CHANNEL^DESCRIPTION";
if ($ curTag == $titleKey) {
$ sTitle = $data;
}
elseif ($ curTag == $linkKey) {
$ sLink = $data;
}
elseif ($ curTag == $descKey) {
$ sDescription = $data;
}

// now get the items
global $ arItems, $ itemCount;
$itemTitleKey = "^ RSS^CHANNEL^ITEM^TITLE";
$itemLinkKey = "^ RSS^CHANNEL^ITEM^LINK";
$itemDescKey = "^ RSS^CHANNEL^ITEM^DESCRIPTION";

if ($ curTag == $itemTitleKey) {
// make new xItem
$ arItems[$ itemCount] = new xItem();

// set new item objects properties
$ arItems[$ itemCount]->xTitle = $data;
}
elseif ($ curTag == $itemLinkKey) {
$ arItems[$ itemCount]->xLink = $data;
}
elseif ($ curTag == $itemDescKey) {
$ arItems[$ itemCount]->xDescription = $data;
// increment item counter
$ itemCount++;
}
}

// main loop
$xml_ parser = xml_ parser_create();
xml_set_element_handler($xml_ parser, "startElement", "endElement");
xml_set_character_data_handler($xml_ parser, " characterData");
if (!($fp = fopen($uFile,"r"))) {
die ("could not open RSS for input");
}
while ($data = fread($fp, 4096)) {
if (!xml_parse($xml_ parser, $data, feof($fp))) {
die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_ parser)), xml_get_current_line_number($xml_ parser)));
}
}
xml_ parser_free($xml_ parser);

// write out the items
?>

for ($i=0;$i $t xItem = $ arItems[$i];
?>
xTitle); ?>

if ($bDesc) {
?>
xDescription); ?>

}
echo ("
");
}

:
ֲ
5 (: 1)

:

  • avatar
    Victoria - 18.02.2013, 15:02
    This site is crazy :)