无法从wiitdb.xml获取SimpleXML的值

时间:2015-09-24 17:23:54

标签: php xml simplexml

我请求帮助,因为我做了所有事情,但我无法获得概要值。我的代码是:

<?php
$xml=simplexml_load_file("wiitdb.xml") or die("Error: Cannot create object");
$b = 'D2SE18';
$a = $xml->xpath("//game/id[.='" . $b ."']/parent::*");
$result = $a[0];

echo "name: " . $result['name'] . "<br>";
echo "ID: " . $result->id . "<br>";
echo "type: " . $result->type . "<br>";
echo "region: " . $result->region . "<br>";
echo "languages: " . $result->languages . "<br>";
echo "title EN: " . $result->locale[0]->title . "<br>";
echo "synopsis EN: " . $result ->locale[0]->children('synopsis', TRUE) . "<br>";
print_r($a);

?>

它的作用是查找ID然后显示该元素的父项值。

当我运行它时,它会显示:

name: Deca Sports 2 (Demo) (USA) (EN,FR,ES)
ID: D2SE18
type: 
region: NTSC-U
languages: EN,FR,ES
title EN: Deca Sports 2 (Demo)
synopsis EN: 

Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => Deca Sports 2 (Demo) (USA) (EN,FR,ES) ) [id] => D2SE18 [type] => SimpleXMLElement Object ( ) [region] => NTSC-U [languages] => EN,FR,ES [locale] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [lang] => EN ) [title] => Deca Sports 2 (Demo) [synopsis] => SimpleXMLElement Object ( ) ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [lang] => ES ) [title] => Deca Sports 2 (Demo) [synopsis] => Prueba tus destrezas deportivas a través de una gran variedad de actividades en Deca Sports 2. Afina tus habilidades en cada deporte en y crea tu propio equipo personalizado con el nuevo editor. Exhibe tus destrezas en una variedad de opciones de un solo jugador o compite con amigos y familiares en los modos multijugador. ) [2] => SimpleXMLElement Object ( [@attributes] => Array ( [lang] => ZHTW ) [title] => 運動大集錦2 試玩版(美) [synopsis] => SimpleXMLElement Object ( ) ) [3] => SimpleXMLElement Object ( [@attributes] => Array ( [lang] => ZHCN ) [title] => 德卡运动会2 试玩版(美) [synopsis] => SimpleXMLElement Object ( ) ) ) [developer] => HUDSON SOFT CO., LTD. [publisher] => Hudson Entertainment, Inc. [date] => SimpleXMLElement Object ( [@attributes] => Array ( [year] => 2009 [month] => 1 [day] => 1 ) ) [genre] => sports [rating] => SimpleXMLElement Object ( [@attributes] => Array ( [type] => ESRB [value] => E ) ) [wi-fi] => SimpleXMLElement Object ( [@attributes] => Array ( [players] => 0 ) ) [input] => SimpleXMLElement Object ( [@attributes] => Array ( [players] => 4 ) [control] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [type] => wiimote [required] => true ) ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [type] => nunchuk [required] => true ) ) ) ) [rom] => SimpleXMLElement Object ( [@attributes] => Array ( [version] => [name] => Deca Sports 2 (Demo) (USA) (EN,FR,ES).iso [size] => 4699979776 ) ) ) )

即使我尝试

echo "synopsis EN: " . $result->locale[0]->synopsis . "<br>";

它没有向我显示值“概要”。

我怎么能得到它?

xml文件的一个示例是:

<game name="Deca Sports 2 (Demo) (USA) (EN,FR,ES)">
        <id>D2SE18</id>
        <type/>
        <region>NTSC-U</region>
        <languages>EN,FR,ES</languages>
        <locale lang="EN">
            <title>Deca Sports 2 (Demo)</title>
            <synopsis>It's a top-ten all over again as Deca Sports 2 offers another all-you-can-play buffet of ten games anyone can enjoy. The all-around collection has something for everyone, with tennis, darts, ice hockey, motorcycle racing, synchronized swimming, speed skating, downhill skiing, bocce, kendo, and dodgeball. Multiple single and multiplayer modes offer tournaments, head-to-head matches, skill contests, and league play. Use the Team Editor to customize everything about your squad, including your team's name, logo, and colors, plus the looks and skills of the players themselves. And, just to make sure the playing field is level, beginners can learn the ropes of any of the sports in Tutorial Mode. (The Demo lets you play 4 sports of the 10!)</synopsis>
        </locale>
        <locale lang="ES">
            <title>Deca Sports 2 (Demo)</title>
            <synopsis/>
        </locale>
        <locale lang="ZHTW">
            <title>運動大集錦2 試玩版(美)</title>
            <synopsis/>
        </locale>
        <locale lang="ZHCN">
            <title>德卡运动会2 试玩版(美)</title>
            <synopsis/>
        </locale>
        <developer>HUDSON SOFT CO., LTD.</developer>
        <publisher>Hudson Entertainment, Inc.</publisher>
        <date year="2009" month="1" day="1"/>
        <genre>sports</genre>
        <rating type="ESRB" value="E"/>
        <wi-fi players="0"/>
        <input players="4">
            <control type="wiimote" required="true"/>
            <control type="nunchuk" required="true"/>
        </input>
        <rom version="" name="Deca Sports 2 (Demo) (USA) (EN,FR,ES).iso" size="4699979776"/>
    </game>

1 个答案:

答案 0 :(得分:0)

在语言环境中,命名空间中没有子节点前缀:

$result->locale[0]->children('synopsis', TRUE)

因此,你没有得到任何输出,它没有孩子:

var_dump($a[0]->children('synopsis', TRUE)->count()); # int(0)

您可能更需要

$result->locale[0]->synopsis;

这是默认命名空间中名为<synopsis>的元素。