zoukankan      html  css  js  c++  java
  • 在php文件中xml格式

    本人是小白,有错误的地方请指正,勿喷!

    在写一个调查问卷的过程中用到了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浏览器上没有那句话 所以只是因为其他浏览器的兼容性而已。

  • 相关阅读:
    git版本控制入门
    SpringBoot入门基础:编写HelloWorld(三)
    SpringBoot入门基础:构建SpringBoot项目及启动器讲解(二)
    SpringBoot入门基础:介绍(一)
    maven入门基础:为项目分配独立的仓库实践(十七)
    maven入门基础:nexus的权限管理(十六)
    maven入门基础:使用maven部署构件到nexus(十五)
    maven入门基础:配置maven从nexus下载构件(十四)
    maven入门基础:nexus构建搜索(十三)
    事务的写法
  • 原文地址:https://www.cnblogs.com/zz-1021/p/9775866.html
Copyright © 2011-2022 走看看