当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > xml_parse() 函数

PHP
PHP 介绍
PHP 安装
PHP 语法
PHP 变量
PHP操作符
PHP If...Else
PHP Switch
PHP 数组
PHP 循环
PHP 函数
PHP 表单
PHP $_GET
PHP $_POST
PHP Date
PHP Include
PHP 文件处理
PHP 文件上传
PHP Cookies
PHP Sessions
PHP 发送邮件

PHP 中的 xml_parse() 函数


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-04   浏览: 435 ::
收藏到网摘: n/a

Definition and Usage
定义和用法

The xml_parse() function parses an XML document.
xml_parse()函数的作用是:解析一个XML文档。

This function returns TRUE on success, or FALSE on failure.
如果这个函数执行成功,将返回True;如果执行失败,将返回False。

Syntax
语法

xml_parse(parser,xml,end)

Parameter
参数
Description
描述
parser Required. Specifies XML parser to use
必要参数。指定需要使用的XML解析器
xml Required. Specifies XML data to parse
必要参数。指定需要解析的XML数据
end Optional. If this parameter is TRUE, the data in the "xml" parameter is the last piece of data sent in this parse.
 可选参数。如果这个参数是True,那么“xml”参数中最后面一部分的数据发送到解析器中

Note: Entity errors are reported at the end of the parse. And will only show if the "end" parameter is TRUE
注意:实体错误将会在解析的最后出示报告;它将显示是否“end”参数是True。



Tips and Notes
提示

Tip: To create an XML parser, use the xml_parser_create() function.
提示:你可以使用xml_parser_create()函数创建一个XML解析器。


Example 1
案例1

XML File
XML文件

<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

PHP Code
PHP代码

<?php
$parser=xml_parser_create();
function char($parser,$data) { echo $data; }
xml_set_character_data_handler($parser,"char");
$fp=fopen("test.xml","r");
while ($data=fread($fp,4096)) { xml_parse($parser,$data,feof($fp)) or 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);
?>

The output of the code above will be:
上述代码将输出下面的结果:

Tove Jani Reminder Don't forget me this weekend!


Example 2
案例2

Using the same XML file but displaying the XML data in another way:
你可以使用相同的XML文件,同时你也可以用其他的方法对它进行显示:

<?php
$parser=xml_parser_create();
function start($parser,$element_name,$element_attrs) { switch($element_name) { case "NOTE": echo "-- Note --<br />"; break; case "TO": echo "To: "; break; case "FROM": echo "From: "; break; case "HEADING": echo "Heading: "; break; case "BODY": echo "Message: "; } }
function stop($parser,$element_name) { echo "<br />"; }
function char($parser,$data) { echo $data; }
xml_set_element_handler($parser,"start","stop");
xml_set_character_data_handler($parser,"char");
$fp=fopen("test.xml","r");
while ($data=fread($fp,4096)) { xml_parse($parser,$data,feof($fp)) or 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);
?>

The output of the code above will be:
上述代码将输出下面的结果:

-- Note --
To: Tove
From: Jani
Heading: Reminder
Message: Don't forget me this weekend!

评论 (0) All

登陆 | 还没注册?