2015-09-24 113 views
0

我请求帮助,因为我做了所有事情,但我无法获得提要值。我的代码是:无法从wiitdb.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

请添加简化但有效的XML代码。 – michi

+0

['children()'方法](http://www.php.net/manual/en/simplexmlelement.children.php)返回特定命名空间中的所有子项,而不是一个特定的节点,但不会看到实际的XML,不可能告诉你实际上试图“回声”的节点。 – IMSoP

+0

添加了示例 –

回答

0

有命名空间前缀大纲中没有孩子的语言环境:

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

因此,你没有得到任何输出,它具有零儿童:

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

你可能更多的是寻找

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

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

+0

我正要回答这个问题,但后来发现,埋在问题中的是他们已经试过这个事实。 – IMSoP