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

时间:2014-08-01 08:20:07

标签: php dom

我正在尝试显示XML文件中的信息。我正在使用PHP及其DOMDocument类。我附加了xml文件和我尝试过的php代码。

<?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>

php代码......

    <?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>";
         }

     }
?>

我只是想输出价格标签值,因为如果能够显示内容不应该是一个问题。最终,我将逐个在HTML结构中显示这些值。

感谢您的帮助。

编辑:此代码输出

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 个答案:

答案 0 :(得分:2)

你这里有两个漏洞。正如另一个答案中所指出的那样,您从错误的元素中获取$prices

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

......应该是:

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

然而,这并没有完全解决问题,因为还存在另一个问题。您循环查看价格,然后始终返回第一个<price>。你需要改变这个:

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

......对此:

$price = $price->nodeValue;

然后更正的代码变为:

<?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>";
    }

}

......输出:

Wash1

剖面线/四门轿车:$ 15

其他:$ 20

Wash2

剖面线/轿车:$ 30

其他:$ 38


您已经问过何时需要使用item()。这主要取决于您是否正在处理set of nodesDOMNodeList)或single nodeDOMNode)。 getElementsByTagName会返回DOMNodeList(可能有很多元素),因此您需要选择一个元素,例如:

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

您可以遍历DOMNodeList(它实现Traversableinterface。当你迭代时,你得到一个DOMNode

$prices = $service->getElementsByTagName('price'); // Returns a `DOMNodeList`
foreach ($prices as $price) { // $price is a `DOMNode`
    // ...
}

答案 1 :(得分:1)

我不打算留下一些代码,但对于xml操作,我更喜欢SimpleXML

可以在php site

找到基本的例子

我放了一些代码:

$xmlstr = <<<XML
<?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>
XML;

$xml = new SimpleXMLElement($xmlstr);

foreach ($xml->service as $key) {
    echo $key->title, "\n";
    foreach ($key->price as $price) {
        echo $price, "\n";
    }
}

输出结果为:

Wash1
Hatch/Sedan:$15
Others:$20
Wash2
Hatch/Sedan:$30
Others:$38

答案 2 :(得分:1)

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

错了 - 您应该获得$ service的元素

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