使用PHP在XML-File中搜索特定值(Searching for a specific value in XML-File using PHP)
我有一点问题:
我有这个XML文件:
<response> <game_count>768</game_count> <games> <message> <appid>730</appid> <playtime_forever>549</playtime_forever> </message> <message> <appid>1300</appid> <playtime_forever>0</playtime_forever> </message> <message> <appid>1309</appid> ....我试图搜索值appid == '730'但遗憾的是没有任何帮助。
EG我试过这个:
$game = new SimpleXMLElement($account_game_data); $res = $game->xpath("games/message/appid[. = 730]"); print_r($res);我的结果应该是<appid>730</appid><playtime_forever>549</playtime_forever></message>作为SimpleXMLElement 。
如果需要任何代码,我会发布它。
I have a little problem:
I have this XML-File:
<response> <game_count>768</game_count> <games> <message> <appid>730</appid> <playtime_forever>549</playtime_forever> </message> <message> <appid>1300</appid> <playtime_forever>0</playtime_forever> </message> <message> <appid>1309</appid> ....I tried to search for the value appid == '730' but unfortunately nothing helped.
E.G i tried this:
$game = new SimpleXMLElement($account_game_data); $res = $game->xpath("games/message/appid[. = 730]"); print_r($res);My result should be <appid>730</appid><playtime_forever>549</playtime_forever></message> as SimpleXMLElement.
If there is any code required, I will post it.
最满意答案
使用以下方法:
$game = new \SimpleXMLElement($account_game_data); $res = $game->xpath('games/message[appid="730"]'); print_r($res[0]->asXML());输出:
<message> <appid>730</appid> <playtime_forever>549</playtime_forever> </message>message[appid="730"] - 匹配具有值为730子appid的message元素
Use the following approach:
$game = new \SimpleXMLElement($account_game_data); $res = $game->xpath('games/message[appid="730"]'); print_r($res[0]->asXML());The output:
<message> <appid>730</appid> <playtime_forever>549</playtime_forever> </message>message[appid="730"] - matches the message element which has child appid with value 730
更多推荐
发布评论