使用PHP在XML

编程入门 行业动态 更新时间:2024-10-28 19:19:32
使用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

更多推荐

本文发布于:2023-08-06 19:17:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1455391.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:PHP   XML

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!