2015-09-24 110 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