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

PHP
PHP 安全邮件
MySQL 介绍
连接 MySQL
创建 MySQL
MySQL 插入记录
MySQL 选择记录
MySQL Where
MySQL Order By
MySQL 记录更新
MySQL 删除记录
PHP ODBC
XML Expat Parser
XML SimpleXML
PHP 数组参考
PHP Calendar
PHP Date
PHP Directory
PHP Filesystem
PHP FTP
PHP HTTP

PHP 中的 xml_parse_into_struct() 函数


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-04   浏览: 556 ::
收藏到网摘: 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

登陆 | 还没注册?