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

PHP
PHP Libxml
PHP Math
PHP Misc
PHP MySQL
PHP SimpleXML
PHP String
PHP XML
PHP Zip
PHP Mail
用PHP5的DirectoryIterators递归扫描目录
PHP 阻止SQL注入式攻击
PHP5面向对象 - 基础 - 类和对象
PHP5面向对象 - 基础 - 类的属性( public )
PHP5面向对象 - 基础 - 类的属性( private )
PHP5面向对象 - 基础 - 方法
PHP5面向对象 - 基础 - 对象的比较
php5面向对象 - 基础 - 构造函数
php5面向对象 - 基础 - 析构函数
用PHP控制用户的浏览器 - ob*函数的使用
PHP PDO 学习笔记

PHP 中的 xml_parse_into_struct() 函数


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

登陆 | 还没注册?