xmlns="http://purl.org/rss/1.0/" xmlns:dc="http://purl.org/dc/elements/1.1/" > freshmeat.net http://freshmeat.net/ freshmeat.net maintains the Web's largest index of Unix and cross-platform open source software. Thousands of applications are meticulously cataloged in the freshmeat.net database, and links to new code are added daily. en-us Technology freshmeat.net freshmeat.net contributors Copyright (c) 1997-2002 OSDN 2002-02-11T10:20+00:00
sloop.splitter 0.2.1 http://freshmeat.net/releases/69583/ A real time sound effects program. 2002-02-11T04:52-06:00
apacompile 1.9.9 http://freshmeat.net/releases/69581/ A full-featured Apache compilation HOWTO. 2002-02-11T04:52-06:00
下面是分析这一文档并显示其中数据的PHP脚本:
// XML file $file = "fm-releases.rdf";
// set up some variables for use by the parser $currentTag = ""; $flag = "";
// create parser $xp = xml_parser_create();
// set element handler xml_set_element_handler($xp, "elementBegin", "elementEnd"); xml_set_character_data_handler($xp, "characterData"); xml_parser_set_option($xp, XML_OPTION_CASE_FOLDING, TRUE);
// read XML file if (!($fp = fopen($file, "r"))) { die("Could not read $file"); }
// parse data while ($xml = fread($fp, 4096)) { if (!xml_parse($xp, $xml, feof($fp))) { die("XML parser error: " . xml_error_string(xml_get_error_code($xp))); } }
// destroy parser xml_parser_free($xp);
// opening tag handler function elementBegin($parser, $name, $attributes) { global $currentTag, $flag; // export the name of the current tag to the global scope $currentTag = $name; // if within an item block, set a flag if ($name == "ITEM") { $flag = 1; } }
// closing tag handler function elementEnd($parser, $name) { global $currentTag, $flag; $currentTag = ""; // if exiting an item block, print a line and reset the flag if ($name == "ITEM") { echo "
"; $flag = 0; } }
// character data handler function characterData($parser, $data) { global $currentTag, $flag; // if within an item block, print item data if (($currentTag == "TITLE" || $currentTag == "LINK" || $currentTag == "DESCRIPTION") && $flag == 1) { echo "$currentTag: $data "; } }
?> 看不明白? 别着急,后面将会作出解释。
捕获旗标
这段脚本首先要做的是设定一些全局变量:
// XML file $file = "fm-releases.rdf";
// set up some variables for use by the parser $currentTag = ""; $flag = "";
// opening tag handler function elementBegin($parser, $name, $attributes) { global $currentTag, $flag; // export the name of the current tag to the global scope $currentTag = $name; // if within an item block, set a flag if ($name == "ITEM") { $flag = 1; } }
// closing tag handler function elementEnd($parser, $name) { global $currentTag, $flag; $currentTag = ""; // if exiting an item block, print a line and reset the flag if ($name == "ITEM") { echo ""; $flag = 0; } } 闭标记处理函数也是以标记名称作为其参数。如果是遇到的是一个为的闭标记,变量$flag的值重置为0,并把变量$currentTag的值清空。
// character data handler function characterData($parser, $data) { global $currentTag, $flag; // if within an item block, print item data if (($currentTag == "TITLE" || $currentTag == "LINK" || $currentTag == "DESCRIPTION") && $flag == 1) { echo "$currentTag: $data "; } }
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn