1. 程式人生 > >在php文件中xml格式

在php文件中xml格式

位置 tex mov 一個 .cn tag file 編碼 了解

本人是小白,有錯誤的地方請指正,勿噴!

在寫一個調查問卷的過程中用到了xml文件,如想要了解,可以通過以下鏈接簡單學習:http://www.w3school.com.cn/xml/

所用工具: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格式