首頁  >  文章  >  後端開發  >  php檔案中xml格式的使用實例

php檔案中xml格式的使用實例

不言
不言轉載
2018-10-12 15:02:253231瀏覽

這篇文章帶給大家的內容是關於php檔案中xml格式的使用實例,有一定的參考價值,有需要的朋友可以參考一下,希望對你有幫助。

在寫問卷的過程中用到了xml文件,如想要了解,可以透過以下連結簡單學習:http://www.php.cn/course/28.html

所用工具:phpstudy notepad (使用utf-8編碼無BOM)

出現的問題如下:最開始的程式碼如下:我想要求出去長沙遊玩的人數是多少人,
<?php
header(&#39;Content-Type: text/xml&#39;);
$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(&#39;Content-Type: text/xml&#39;);
$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中文網其他相關文章!

陳述:
本文轉載於:cnblogs.com。如有侵權,請聯絡admin@php.cn刪除