四、php对xml的支持
php对xml提供了的强大的支持。它使用了一个xml的“解析器”,并且为了支持这个解析器,它提供了20(php4)个xml的解析函数。下面是几个最常用的php解析函数。
1. xml_parse
boolean xml_parse(int parser, string data, int [isfinal]);
本函数用来解析 xml 格式的文件资料。参数 parser 为解析代码。参数 data 为解析的资料区块 (chunk)。参数 isfinal 可省略,若设为 true 则系统会自动送出最后的资料部分 (piece) 给 data 参数。若无错误则返回 true 值。
2. xml_parser_create
int xml_parser_create(string [encoding]);
本函数用来初始化一个新的 xml 解析器。参数 encoding 可省略,为 xml 使用的字符集,默认值为 iso-8859-1,其它尚有 us-ascii、utf-8 二种。成功则返回 parser 代码供其它函数使用,失败则返回 false 值。
3. xml_set_element_handler
boolean xml_set_element_handler(int parser, string startelementhandler, string endelementhandler);
本函数配置元素的标头供 xml_parse() 函数使用。参数 parser 为解析代码。参数 startelementhandler 及 endelementhandler 分别为元素开始与结束的标头,其中的 startelementhandler 必须包括解析代码、名称、与属性,而 endelementhandler 参数包括了解析代码及名称二个参数。若无错误则返回 true 值。
4. xml_set_character_data_handler
boolean xml_set_character_data_handler(int parser, string handler);
本函数配置字符资料的标头。参数 parser 为解析代码。参数 handler 包括解析代码及资料字符串等二个元素。若无错误则返回 true 值。
5. xml_get_error_code
int xml_get_error_code(int parser);
本函数可取得 xml 在处理时的错误代码。参数 parser 为解析代码。若 parser 有错则返回 false 值,否则就返回错误代码 (如 xml_error_binary_entity_ref .... 等等)。
6. xml_error_string
string xml_error_string(int code);
本函数可取得 xml 在处理时的错误代码。参数 code 为解析错误代码。若无错误返回值为代码的文字描述字符串。
7. xml_get_current_line_number
int xml_get_current_line_number(int parser);
本函数用来取得目前 xml 解析所正在处理的行号。参数 parser 为解析代码。若 parser 有错则返回 false 值,若无错误则返回行号数字。
8. xml_parser_free
boolean xml_parser_free(int parser);
本函数用来释放目前 xml 解析所使用的内存。参数 parser 为解析代码。若没有错误则返回 true 值,否则返回 false 值。
五、案例解析
以下是用php5实现读取一个以xml 1.0格式编写的通讯录address.xml,并显示其内容的示例。详解见有关注释。
<?
//
//第一部分:几个php helper函数
//
/**********************************
*从一个文件中读取xml的 *
*内容到字符串中 *
*********************************/
function read_file($filename)
{
//读取文件
$lines=file($filename);
//变量$contents是存放文件内容的变量
$contents=;
while(list($key,$value)=each($lines))
{
$contents.=$value;
}
return $contents;
}
/*********************************
*当php遇到一个xml的起始标记时 *
*调用,其作用是按照一定的层次 *
*显示出xml的标记 *
*********************************/
function start_element($parser, $name, $attrs)
{
//变量$depth存放标记的深度
global $depth;
//变量$spacer存放标记前所有的箭头符号
$spacer=;
for ($i=1;$i<$depth[$parser];$i++)
{
$spacer.=->;
}
//设置标记显示时的颜色
if($depth[$parser]==0)
{
$font_color=red;
}
else
if($depth[$parser]==1)
{
$font_color=green;
}
else
{
$font_color=blue;
}
//设置标记显示时的字体
$font_size=5-$depth[$parser];
if ($font_size<2)
{
$font_size=2;
}
//显示标记
echo <font size=.$font_size. color=.$font_color.>;
echo $spacer.$depth[$parser];
if ($depth[$parser]<>0)
{
echo 、;
}
echo $name.<br>;
echo </font>;
$depth[$parser]++;
//如果是主标记就显示红色的水平线
if($depth[$parser]==2)
{
echo <hr color=red size=1>;
}
}
/*********************************
*当php遇到一个xml的结束标记时 *
*调用,其作用是改变当前的层次 *
*计数,并在主标记下显示水平线 *
*********************************/
function stop_element($parser,$name)
{
//变量$depth存放标记的深度
global $depth;
$depth[$parser]--;
//如果是主标记就显示红色的水平线
if($depth[$parser]==2)
{
echo <hr color=red size=1>;
}
}
/*********************************
*当php遇到一个xml的标记内容时 *
*调用,其作用是按照一定的层次 *
*显示出标记的内容 *
*********************************/
function char_data($parser,$data)
{
//变量$depth存放标记的深度
global $depth;
//显示标记的内容
$data=trim($data);
if (strlen($data))
{
for ($i=1;$i<$depth[$parser]+6;$i++)
echo
echo <b>$data</b><br>\\n;
}
}
//
//第二部分:php文件开始执行处
//
//要解析的xml文件的名字
$file=address.xml;
//读取文件
$data=read_file($file);
// 产生解析器的实例
$parser = xml_parser_create();
// 设置处理函数
xml_set_element_handler($parser, start_element, stop_element);
xml_set_character_data_handler($parser, char_data);
// 解析文件
if(!xml_parse($parser,$data,1))
{
//报错
die(sprintf(xml error: %s at line %d,
xml_error_string(xml_get_error_code($parser)),
xml_get_current_line_number($parser)));
}
// 释放解析器
xml_parser_free($parser);
?>
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器