登录  /  注册
php文件中xml格式的使用实例
不言
发布: 2023-04-04 09:44:01
转载
2527人浏览过

本篇文章给大家带来的内容是关于php文件中xml格式的使用实例,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

在写一个调查问卷的过程中用到了xml文件,如想要了解,可以通过以下链接简单学习://m.sbmmt.com/course/28.html

所用工具:phpstudy+notepad++(使用utf-8编码无BOM)

出现的问题如下:最开始的代码如下:我想要 求出去长沙游玩的人数是多少人,
<?php
header('Content-Type: text/xml');
$xmlstr="<?xml version=\"1.0\" encoding=\"utf-8\"?> 
<movies>
<movie>
<title>旅游的人数</title>
<id>1</id>
<count>350</count>
<content>长沙</content>
</movie>
<movie>
<title>旅游的人数</title>
<id>2</id>
<count>47</count>
<content>张家界</content>
</movie>
<movie>
<title>旅游的人数</title>
<id>3</id>
<count>118</count>
<content>上海</content>
</movie>
<movie>
<title>旅游的人数</title>
<id>4</id>
<count>80</count>
<content>南京</content>
</movie>
</movies>
";
$xml = simplexml_load_string($xmlstr);
$count0 = $xml->movie[0]->count;
echo $count0;
?>
登录后复制

火狐浏览器上:

XML 解析错误:语法错误
位置:http://localhost:8080/diaocha/2.php
行 1,列 1:350

360浏览器上的错误:

This page contains the following errors:

error on line 1 at column 1: Document is empty

Below is a rendering of the page up to the first error

需要改正的地方是:

去掉第一句:header('Content-Type: text/xml');即可。得到350

2、如果你是需要查看xml格式的时候

代码如下:

<?php
header('Content-Type: text/xml');
$xmlstr="<?xml version=\"1.0\" encoding=\"utf-8\"?> 
<movies>
<movie>
<title>旅游的人数</title>
<id>1</id>
<count>350</count>
<content>长沙</content>
</movie>
<movie>
<title>旅游的人数</title>
<id>2</id>
<count>47</count>
<content>张家界</content>
</movie>
<movie>
<title>旅游的人数</title>
<id>3</id>
<count>118</count>
<content>上海</content>
</movie>
<movie>
<title>旅游的人数</title>
<id>4</id>
<count>80</count>
<content>南京</content>
</movie>
</movies>
";
登录后复制

就可以得出结果:

This XML file does not appear to have any style information associated with it. The document tree is shown below.

<movies>
<movie>
<title>旅游的人数</title>
<id>1</id>
<count>350</count>
<content>长沙</content>
</movie>
<movie>
<title>旅游的人数</title>
<id>2</id>
<count>47</count>
<content>张家界</content>
</movie>
<movie>
<title>旅游的人数</title>
<id>3</id>
<count>118</count>
<content>上海</content>
</movie>
<movie>
<title>旅游的人数</title>
<id>4</id>
<count>80</count>
<content>南京</content>
</movie>
</movies>
";
登录后复制

上方的那句话可以不用管:在IE浏览器上没有那句话 所以只是因为其他浏览器的兼容性而已。

以上就是php文件中xml格式的使用实例的详细内容,更多请关注php中文网其它相关文章!

相关标签:
来源:博客园网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 技术文章
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2023 //m.sbmmt.com/ All Rights Reserved | 苏州跃动光标网络科技有限公司 | 苏ICP备2020058653号-1

 | 本站CDN由 数掘科技 提供

登录PHP中文网,和优秀的人一起学习!
全站2000+教程免费学