duanlu0386 2014-08-01 08:20
浏览 35
已采纳

PHP - 尝试显示xml标记(具有相同名称的标记)nodevalue

I am trying to display information from a XML file. I am using PHP and its DOMDocument class. I am attaching the xml file and the php code that i have tried.

<?xml version="1.0" encoding="utf-8" ?>
<services>
<service>
    <type>Wash</type>
    <title>Wash1</title>
    <content>Full Exterior Hand Wash</content>
    <content>Chamois dry, Pressure Clean Wheels, All Door Jambs</content>   
    <content>Plus Free Typre Gloss</content>
    <price>Hatch/Sedan:$15</price>
    <price>Others:$20</price>
</service>

<service>
    <type>Wash</type>
    <title>Wash2</title>
    <content>Wash1</content>
    <content>Plus Vacuum</content>   
    <content>Glass Cleaned(IN/OUT)</content>
    <content>Plus Free Interior Wipe</content>
    <price>Hatch/Sedan:$30</price>
    <price>Others:$38</price>
 </service>
 </services>

The php code...

    <?php
    header('Content-Type: text/html;charset=utf8');

    $xmlfile = new DOMDocument();
    $xmlfile->load('services.xml');

    $services = $xmlfile->getElementsByTagName('service');

    foreach($services as $service){
         $titles = $service->getElementsByTagName('title');
         $title = $titles->item(0)->nodeValue;
         echo "<h1>$title</h1>";

         $prices = $xmlfile->getElementsByTagName('price');

         foreach($prices as $price){
             $price = $prices->item(0)->nodeValue;
             echo "<h1>$price</h1>";
         }

     }
?>

I am only trying to output price tag values, since if that works displaying content shouldn't be a problem. Eventually i will be displaying these values in HTML structure, one by one.

Thanks for your help.

Edit: This code at the moment outputs

Wash1
Hatch/Sedan:$15
Hatch/Sedan:$15
Hatch/Sedan:$15
Hatch/Sedan:$15
Wash2
Hatch/Sedan:$15 
Hatch/Sedan:$15
Hatch/Sedan:$15
Hatch/Sedan:$15
  • 写回答

3条回答 默认 最新

  • doudai8783 2014-08-01 08:34
    关注

    You've got two bugs here. As pointed out in another answer, you're getting $prices from the wrong element:

    $prices = $xmlfile->getElementsByTagName('price');
    

    ... should be:

    $prices = $service->getElementsByTagName('price');
    

    This doesn't fix the problem completely, however, as there's another problem. You're looping through the prices but then always returning the first <price>. You need to change this:

    $price = $prices->item(0)->nodeValue;
    

    ... to this:

    $price = $price->nodeValue;
    

    The corrected code then becomes:

    <?php
    header('Content-Type: text/html;charset=utf8');
    
    $xmlfile = new DOMDocument();
    $xmlfile->load('services.xml');
    
    $services = $xmlfile->getElementsByTagName('service');
    
    foreach($services as $service){
         $titles = $service->getElementsByTagName('title');
         $title = $titles->item(0)->nodeValue;
         echo "<h1>$title</h1>";
    
        $prices = $service->getElementsByTagName('price');
    
        foreach($prices as $price){
            $price = $price->nodeValue;
            echo "<h1>$price</h1>";
        }
    
    }
    

    ... which outputs:

    Wash1

    Hatch/Sedan:$15

    Others:$20

    Wash2

    Hatch/Sedan:$30

    Others:$38


    You've asked when it's necessary to use item(). This basically depends on whether you're dealing with a set of nodes (a DOMNodeList) or a single node (DOMNode). getElementsByTagName returns a DOMNodeList (there might be lots of elements) so you need to select a single one, e.g.:

     $titles = $service->getElementsByTagName('title'); // Returns a `DOMNodeList` with all the titles
     $title = $titles->item(0)->nodeValue;              // Grabs just the first title
    

    You can iterate through a DOMNodeList (it implements the Traversable) interface. When you iterate you get a DOMNode back:

    $prices = $service->getElementsByTagName('price'); // Returns a `DOMNodeList`
    foreach ($prices as $price) { // $price is a `DOMNode`
        // ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100