【问题标题】:Parsing xml file with .php extension and php header解析带有 .php 扩展名和 php 标头的 xml 文件
【发布时间】:2012-04-17 13:07:06
【问题描述】:

我有一个小问题。我需要解析一个文件并用结果填充一个网页横幅。问题是,该文件名为:“_banner_products.php”,其内容如下:

<?php header('Content-Type:text/xml'); ?><?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<carouselle>
 <firstLayer>
   <layerName>Leica Disto X310</layerName>
   <layerProduct>Disto X310</layerProduct>
   <layerPic>http://www.leicaestonia.ee/extensions/boxes_design/flashpics/1334482548.jpg</layerPic>
   <layerPrice>0,-</layerPrice>
   <layerPriceOld></layerPriceOld>
   <layerLink>http://www.leicaestonia.ee/index.php?id=11627</layerLink>
   <layerTimer>01.05.2012 00:00</layerTimer>
 </firstLayer>
 <firstLayer>
   .....
   .....
</firstLayer>
</carouselle>

如何循环遍历这个文件以将所有“firstLayer”子级分组为一个,依此类推.. 如果我只是使用:

$file = fopen("_banner_products.php", "r");
while (!feof($file)){
    $line = fgets($file);
}

simplexml_load_file 抛出这个- "_banner_products.php:1: 解析器错误:需要开始标签,'

然后我只得到 <...> 标记的内容,这意味着我无法区分我是否已经超出范围。

感谢任何人的回复。如果有什么不清楚的地方,我会试着解释更多。

编辑。 感谢您的解决方案,确实使用完整的 URL 有效:

simplexml_load_file("http://localhost/MySite/_banner_products.php");

【问题讨论】:

    标签: php xml-parsing


    【解决方案1】:

    您遇到问题是因为 simplexml_load_file 将您的文件视为本地 xml 文件.. 您需要做的是添加完整的 URL

    例子

     simplexml_load_file("http://localhost/web/_banner_products.php"); 
    

    例如获取layerName 的用例

    _banner_products.php

    <?php
    header ( 'Content-Type:text/xml' );
    echo '<?xml version="1.0" encoding="UTF-8"?>';
    ?>
    <carouselle>
     <firstLayer>
       <layerName>Leica Disto X310</layerName>
       <layerProduct>Disto X310</layerProduct>
       <layerPic>http://www.leicaestonia.ee/extensions/boxes_design/flashpics/1334482548.jpg</layerPic>
       <layerPrice>0,-</layerPrice>
       <layerPriceOld></layerPriceOld>
       <layerLink>http://www.leicaestonia.ee/index.php?id=11627</layerLink>
       <layerTimer>01.05.2012 00:00</layerTimer>
     </firstLayer>
     <firstLayer>
       <layerName>Leica Disto X310</layerName>
       <layerProduct>Disto X310</layerProduct>
       <layerPic>http://www.leicaestonia.ee/extensions/boxes_design/flashpics/1334482548.jpg</layerPic>
       <layerPrice>0,-</layerPrice>
       <layerPriceOld></layerPriceOld>
       <layerLink>http://www.leicaestonia.ee/index.php?id=11627</layerLink>
       <layerTimer>01.05.2012 00:00</layerTimer>
     </firstLayer>
    </carouselle>
    

    查看详情

    $xml = simplexml_load_file("http://localhost/lab/stockoverflow/_banner_products.php");
    
    echo "<pre>" ;
    foreach($xml as $key => $element)
    {
        echo $element->layerName , PHP_EOL ;
    }
    

    【讨论】:

    • 除此之外,要遍历 XML,您应该使用类似于示例 #4 的 php.net/manual/en/simplexml.examples-basic.php
    • 如果我把完整路径http://localhost/MySite/_banner_products.php它给了我I/O warning : failed to load external entity "/_banner_products.php"
    【解决方案2】:

    最明显的方法是去掉第一行,然后在代码中重新添加 XML 声明。

    您也可以使用 PHP 解析文件,使用 eval(),但要非常确定您正在解析什么,因为这可能是一个非常大的安全漏洞。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-14
      • 1970-01-01
      • 1970-01-01
      • 2018-05-24
      • 2016-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多