php文件中xml格式的使用实例
来源:不言
发布时间:2018-10-15 15:08:56
阅读量:854
本篇文章给大家带来的内容是关于php文件中xml格式的使用实例,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
在写一个调查问卷的过程中用到了xml文件,如想要了解,可以通过以下链接简单学习:http://www.php.cn/course/28.html
所用工具:phpstudy+notepad++(使用utf-8编码无BOM)
出现的问题如下:最开始的代码如下:我想要 求出去长沙游玩的人数是多少人,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | <?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格式的时候
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | <?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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <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>
";
|