php中xml解析函数xml_parser_create
xml 解析器。
语法: int xml_parser_create(string [encoding]);
返回值: 整数
函数种类: 资料处理
内容说明
本函数用来初始化一个新的 xml 解析器。参数 encoding 可省略,为 xml 使用的字符集,默认值为 iso-8859-1,其它尚有 us-ascii、utf-8 二种。成功则返回 parser 代码供其它函数使用,失败则返回 false 值。
*/
$xmlfile='test.xml'; //定义一个xml文件
$xmlparser=xml_parser_create(); //建立一个xml解析器
$fp=fopen($xmlfile,'r'); //打开一个文件并读取数据
while($xmldata=fread($fp,4096)) //循环读取文件内容
{
if(!xml_parse($xmlparser,$xmldata,feof($fp))) //解析xml数据,如果失败输出错误信息
{
die(print "error:".
xml_error_string(xml_get_error_code($xmlparser))."<br/>"."line:".
xml_get_current_line_number($xmlparser)."<br/>"."column:".
xml_get_current_column_number($xmlparser)."<br/>");
}
}
xml_parser_free($xmlparser); //释放xml解析器
/*
php 5 开始,自动侦测输入的 xml 的编码,因此 encoding 参数仅用来指定解析后输出数据的编码
在 php 5.0.0 和 5.0.1 总,默认输出的字符编码是 iso-8859-1,而 php 5.0.2 及以上版本是 utf-8。解析器支持的编码有 iso-8859-1, utf-8 和 us-ascii
bool xml_parser_free ( resource parser )
parser
指向要释放的 xml 解析器的指针。
如果 parser 参数没有指向一个合法的解析器,该函数将返回 false,否则将释放指定的解析器并返回 true
*/
$simple="<para><note>simple note</note></para>"; //定义xml数据
$p=xml_parser_create(); //创建xml解析器
xml_parse_into_struct($p,$simple,$vals,$index); //将数据解析到数组
xml_parser_free($p); //释放xml解析器
echo "index arrayn";
print_r($index); //输出结果数组
echo "nvals arrayn";
print_r($vals);
/*输出结果为:
index array
array
(
[para] => array
(
[0] => 0
[1] => 2
)
[note] => array
(
[0] => 1
)
)
vals array
array
(
[0] => array
(
[tag] => para
[type] => open
[level] => 1
)
[1] => array
(
[tag] => note
[type] => complete
[level] => 2
[value] => simple note
)
[2] => array
(
[tag] => para
[type] => close
[level] => 1
)
)
*/
$xml=(substr($response,strpos($response,"rnrn")+4)); //定义xml内容
$data=xmlrpc_decode_request($xml,$method); //对xml译码
echo "method returned was:$method<hr/>n"; //输出结果
echo "<pre>";
var_dump($data);
echo "</pre>n";
//
$xml=(substr($response,strpos($response,"rnrn")+4)); //定义xml内容
$php教程vars=xmlrpc_decode($xml); //将xml译码
//
$params="system.methodsignature"; //定义数据
$method="system.methodhelp"; //定义method
$request=xmlrpc_encode_request($method,$params); //生成xml
echo $request;
//
$params=1; //定义php值
$response=xmlrpc_encode($params); //为php值生成xml
echo $response; //输出结果
分别是创建、增加、删除、修改四个功能,变量都是写死的,改一改用$_post方式接收就可以用了
//index.php教程 创建功能
复制代码 代码如下:
<?php
$xmlpatch = 'index.xml';
$_id = '1';
$_title = 'title1';
$_content = 'content1';
$_author = 'author1';
$_sendtime = 'time1';
$_htmlpatch = '1.html';
111cn.net$doc = new domdocument('1.0', 'utf-8');
$doc -> formatoutput = true;
111cn.net$root = $doc -> createelement('root');//新建节点
111cn.net$index = $doc -> createelement('index');//新建节点
111cn.net$url = $doc -> createattribute('url');//新建属性
$patch = $doc -> createtextnode($_htmlpatch);//新建text值
$url -> appendchild($patch);//将$patch文本设为$url属性的值
111cn.net$id = $doc -> createattribute('id');
$newsid = $doc -> createtextnode($_id);
$id -> appendchild($newsid);
111cn.net$title = $doc -> createattribute('title');
$newstitle = $doc -> createtextnode($_title);
$title -> appendchild($newstitle);
111cn.net$content = $doc -> createtextnode($_content);//节点值
111cn.net$author = $doc -> createattribute('author');
$newsauthor = $doc -> createtextnode($_author);
$author -> appendchild($newsauthor);
111cn.net$sendtime = $doc -> createattribute('time');
$newssendtime = $doc -> createtextnode($_sendtime);
$sendtime -> appendchild($newssendtime);
111cn.net$index -> appendchild($id);//将$id设为index节点的属性,以下类同
$index -> appendchild($title);
$index -> appendchild($content);
$index -> appendchild($url);
$index -> appendchild($author);
$index -> appendchild($sendtime);
111cn.net$root -> appendchild($index);//设置index为root字节点
111cn.net$doc -> appendchild($root);//设置root为跟节点
111cn.net$doc -> save($xmlpatch);//保存文件
111cn.netecho $xmlpatch . ' has create success';
111cn.net?>
111cn.net<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>xml操作</title>
</head>
111cn.net<body>
</body>
</html>
//add.php 增加功能(跟index.php文件差不多,主要就是加个load载入跟 $root = $doc -> documentelement获得跟节点
复制代码 代码如下:
<?php
$xmlpatch = 'index.xml';
$_id = '2';
$_title = 'title2';
$_content = 'content2';
$_author = 'author2';
$_sendtime = 'time2';
$_htmlpatch = '2.html';
111cn.net$doc = new domdocument();
$doc -> formatoutput = true;
if($doc -> load($xmlpatch)) {
$root = $doc -> documentelement;//获得根节点(root)
$index = $doc -> createelement('index');
111cn.net$url = $doc -> createattribute('url');
$patch = $doc -> createtextnode($_htmlpatch);
$url -> appendchild($patch);
111cn.net$id = $doc -> createattribute('id');
$newsid = $doc -> createtextnode($_id);
$id -> appendchild($newsid);
111cn.net$title = $doc -> createattribute('title');
$newstitle = $doc -> createtextnode($_title);
$title -> appendchild($newstitle);
111cn.net$content = $doc -> createtextnode($_content);
111cn.net$author = $doc -> createattribute('author');
$newsauthor = $doc -> createtextnode($_author);
$author -> appendchild($newsauthor);
111cn.net$sendtime = $doc -> createattribute('time');
$newssendtime = $doc -> createtextnode($_sendtime);
$sendtime -> appendchild($newssendtime);
111cn.net$index -> appendchild($id);
$index -> appendchild($title);
$index -> appendchild($content);
$index -> appendchild($url);
$index -> appendchild($author);
$index -> appendchild($sendtime);
111cn.net$root -> appendchild($index);
111cn.net$doc -> save($xmlpatch);
111cn.netecho $_id . ' has been added in ' . $xmlpatch;
111cn.net} else {
echo 'xml file loaded error!';
}
?>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>xml操作-添加</title>
</head>
111cn.net<body>
</body>
</html>
//edit.php 修改功能(这里只修改title属性值 跟节点值)
复制代码 代码如下:
<?php
$xmlpatch = 'index.xml';
$_id = '2';
$_title = 'has been changed';
$_content = 'has been changed';
111cn.net$doc = new domdocument();
$doc -> formatoutput = true;
111cn.netif($doc -> load($xmlpatch)) {
$root = $doc -> documentelement;
$elm = $root -> getelementsbytagname('index');
$checkexist = 0;
foreach ($elm as $new) {
if($new -> getattribute('id') == $_id) {
$new -> setattribute('title', $_title);
$new -> nodevalue = $_content;//修改节点值,真是太意外了,没想到跟js一样直接能赋值...
//$new -> removechild($new -> nodevalue);
$checkexist = 1;
}
}
if($checkexist == 0) {
echo $_id . ' is not found in ' . $xmlpatch;
} else {
$doc -> save($xmlpatch);
echo $_id . ' has been changed';
}
} else {
echo 'xml file loaded error!';
}
111cn.net?>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>xml操作-修改</title>
</head>
111cn.net<body>
</body>
</html>
//del.php 删除功能
复制代码 代码如下:
<?php
$xmlpatch = 'index.xml';
$_id = '2';
111cn.net$doc = new domdocument();
$doc -> formatoutput = true;
if($doc -> load($xmlpatch)) {
$root = $doc -> documentelement;
$elm = $root -> getelementsbytagname('index');
foreach ($elm as $new) {
if($new -> getattribute('id') == $_id) {
if($root -> removechild($new)) {
echo $_id . ' has been deleted';
} else {
echo $_id . ' delete failed';
}
}
}
$doc -> save($xmlpatch);
} else {
echo 'xml file loaded error!';
}
111cn.net?>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>xml操作-删除</title>
</head>
111cn.net<body>
</body>
</html>
xml文档格式如下
<?xml version="1.0" encoding="utf-8"?>
<list>
<company>武汉xxx公司</company>
<user>
<name>张三</name>
<age sex="未知">a</age>
<height>1</height>
</user>
<user>
<name>李四</name>
<age sex="女">b</age>
<height>2</height>
</user>
<user>
<name>王五</name>
<age sex="男">c</age>
<height>3</height>
</user>
<town parent="0" id="1">台北</town>
<town parent="1" id="2">板桥</town>
<town parent="0" id="3">桃园</town>
</list>
php解析代码
*/
header("content-type:text/html; charset=utf-8"); //设置编码
$xml = simplexml_load_file('a.xml'); //载入xml文件 $lists和xml文件的根节点是一样的
echo $xml->company."<br>";
echo $xml->town."<br>id:";
echo $xml->town['id']."<br>parent:";
echo $xml->town['parent']."<br>";
echo "<br>循环读取:<br>";
foreach($xml->user as $users){ //有多个user,取得的是数组,循环输出
echo "-------------------<br>";
echo "姓名:".$users->name."<br>";
echo "编号:".$users->age."<br>";
echo "性别:".$users->age['sex']."<br>";
echo "序号:".$users->height."<br>";
}
echo "<br>循环读取:<br>";
foreach($xml->town as $towns){ //有多个user,取得的是数组,循环输出
echo "-------------------<br>";
echo "id:".$towns['id']."<br>";
echo "归属:".$towns['parent']."<br>";
echo "地区:".$towns."<br>";
}
/*
定义和用法
simplexml_load_file() 函数把 xml 文档载入对象中。
如果失败,则返回 false。
语法
simplexml_load_file(file,class,options,ns,is_prefix)参数 描述
file 必需。规定要使用的 xml 文档。
class 可选。规定新对象的 class。
options 可选。规定附加的 libxml 参数。
ns 可选。
is_prefix 可选。
返回值
返回类 simplexmlelement 的一个对象,该对象的属性包含 xml 文档中的数据。如果失败,则返回 false
代码如下 | 复制代码 |
$sql = "查询数据库文件"; echo "<photo desc='$result[文件名字段]' url='_pics/$result[文件名字段]' />"; } function _delimage($path){ //利用domdocument ?> |
相关文章
- eval函数在php中是一个函数并不是系统组件函数,我们在php.ini中的disable_functions是无法禁止它的,因这他不是一个php_function哦。 eval()针对php安全来说具有很...2016-11-25
- 在php中eval是一个函数并且不能直接禁用了,但eval函数又相当的危险了经常会出现一些问题了,今天我们就一起来看看eval函数对数组的操作 例子, <?php $data="array...2016-11-25
Python astype(np.float)函数使用方法解析
这篇文章主要介绍了Python astype(np.float)函数使用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-06-08- 这篇文章主要介绍了Python中的imread()函数用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-16
- 本文主要介绍了C# 中取绝对值的函数。具有很好的参考价值。下面跟着小编一起来看下吧...2020-06-25
- 下面小编就为大家带来一篇C#学习笔记- 随机函数Random()的用法详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
- 一、常量常量是一个简单值的标识符(名字)。如同其名称所暗示的,在脚本执行期间该值不能改变(除了所谓的魔术常量,它们其实不是常量)。常量默认为大小写敏感。按照惯例常量标识符总是大写的。 常量名和其它任何 PHP 标签遵循...2015-10-30
- 这篇文章主要介绍了JavaScript预解析,对象的的相关资料,小编觉得这篇文章写的还不错,需要的朋友可以参考下,希望能够给你带来帮助...2021-11-10
- CREATE FUNCTION ChangeBigSmall (@ChangeMoney money) RETURNS VarChar(100) AS BEGIN Declare @String1 char(20) Declare @String2 char...2016-11-25
- 这篇文章主要介绍了C++中Sort函数详细解析,sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变...2022-08-18
Android开发中findViewById()函数用法与简化
findViewById方法在android开发中是获取页面控件的值了,有没有发现我们一个页面控件多了会反复研究写findViewById呢,下面我们一起来看它的简化方法。 Android中Fin...2016-09-20- strstr() 函数搜索一个字符串在另一个字符串中的第一次出现。该函数返回字符串的其余部分(从匹配点)。如果未找到所搜索的字符串,则返回 false。语法:strstr(string,search)参数string,必需。规定被搜索的字符串。 参数sea...2013-10-04
PHP函数分享之curl方式取得数据、模拟登陆、POST数据
废话不多说直接上代码复制代码 代码如下:/********************** curl 系列 ***********************///直接通过curl方式取得数据(包含POST、HEADER等)/* * $url: 如果非数组,则为http;如是数组,则为https * $header:...2014-06-07- Foreach 函数(PHP4/PHP5)foreach 语法结构提供了遍历数组的简单方式。foreach 仅能够应用于数组和对象,如果尝试应用于其他数据类型的变量,或者未初始化的变量将发出错误信息。...2013-09-28
- free函数是释放之前某一次malloc函数申请的空间,而且只是释放空间,并不改变指针的值。下面我们就来详细探讨下...2020-04-25
- PHP 函数 strip_tags 提供了从字符串中去除 HTML 和 PHP 标记的功能,该函数尝试返回给定的字符串 str 去除空字符、HTML 和 PHP 标记后的结果。由于 strip_tags() 无法实际验证 HTML,不完整或者破损标签将导致更多的数...2014-05-31
SQL Server中row_number函数的常见用法示例详解
这篇文章主要给大家介绍了关于SQL Server中row_number函数的常见用法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-12-08- 分享一个PHP加密解密的函数,此函数实现了对部分变量值的加密的功能。 加密代码如下: /* *功能:对字符串进行加密处理 *参数一:需要加密的内容 *参数二:密钥 */ function passport_encrypt($str,$key){ //加密函数 srand(...2015-10-30
php的mail函数发送UTF-8编码中文邮件时标题乱码的解决办法
最近遇到一个问题,就是在使用php的mail函数发送utf-8编码的中文邮件时标题出现乱码现象,而邮件正文却是正确的。最初以为是页面编码的问题,发现页面编码utf-8没有问题啊,找了半天原因,最后找到了问题所在。 1.使用 PEAR 的...2015-10-21- 下面小编就为大家带来一篇C#中加载dll并调用其函数的实现方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25