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

PHP
WINDOWS下安装MySQL
PHP 制作 网站/服务器 监视脚本
用PHP和CSS制作活动按钮
PHP 单件模式
PHP MVC模式,类封装以及HACK
PHP 中使用正则表达式
PHP 防止 SQL 注入攻击
PHP 跨站点脚本攻击
PHP 防止用户操纵 GET 变量
PHP 防止远程表单提交

PHP 中的 xml_parse_into_struct() 函数


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

Definition and Usage
定义和用法

The xml_parse_into_struct() function parses XML data into an array.
xml_parse_into_struct()函数的作用是:将XML数据解析到一个数组中。

This function parses the XML data into 2 arrays:
这个函数将XML数据解析成2个数组:

  • Value array - containing the data from the parsed XML
    Value array –包含了解析的XML数据
  • Index array - containing pointers to the location of the values in the Value array
    Index array –包含了定位数组值的指针

This function returns 1 on success, or 0 on failure.
如果函数成功执行,将返回1;如果执行失败,将返回0。

Syntax
语法

xml_parse_into_struct(parser,xml,value_arr,index_arr)

Parameter
参数
Description
描述
parser Required. Specifies XML parser to use
必要参数。指定需要解析的XML
xml Required. Specifies XML data to parse
必要参数。指定需要解析的XML数据
value_arr Required. Specifies the target array for the XML data
必要参数。指定XML数据的目标数组[the target array for the XML data]
index_arr Optional. Specifies the target array for index data
可选参数。指定索引数据的目标数组[the target array for index data]


Tips and Notes
注意点

Note: The xml_parse_into_struct() function returns 1 for success and 0 for failure.  This is not the same as TRUE and FALSE.
注意:xml_parse_into_struct()函数如果成功执行,将返回1(不是True);如果执行失败,将返回0(不是False)。


Example
案例

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
//invalid xml file
$xmlfile = 'test.xml';
$xmlparser = xml_parser_create();
// open a file and read data
$fp = fopen($xmlfile, 'r');
$xmldata = fread($fp, 4096);
xml_parse_into_struct($xmlparser,$xmldata,$values);
xml_parser_free($xmlparser);
print_r($values);
?>

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

Array
(
[0] => Array ( [tag] => NOTE [type] => open [level] => 1 [value] => ) [1] => Array ( [tag] => TO [type] => complete [level] => 2 [value] => Tove )
[2] => Array ( [tag] => NOTE [value] => [type] => cdata [level] => 1 ) [3] => Array ( [tag] => FROM [type] => complete [level] => 2 [value] => Jani )
[4] => Array ( [tag] => NOTE [value] => [type] => cdata [level] => 1 ) [5] => Array ( [tag] => HEADING [type] => complete [level] => 2 [value] => Reminder )
[6] => Array ( [tag] => NOTE [value] => [type] => cdata [level] => 1 ) [7] => Array ( [tag] => BODY [type] => complete [level] => 2 [value] => Don't forget me this weekend! )
[8] => Array ( [tag] => NOTE [value] => [type] => cdata [level] => 1 )
[9] => Array ( [tag] => NOTE [type] => close [level] => 1 )
)

评论 (0) All

登陆 | 还没注册?