开发者

Replace element value in XML php

开发者 https://www.devze.com 2023-02-03 02:39 出处:网络
Hey I am looking for a way to replace an element value with another. What I have in mind is when a variable is equal to \"Assass开发者_开发问答in\'s Creed: Brotherhood\", this value is replaced by a n

Hey I am looking for a way to replace an element value with another. What I have in mind is when a variable is equal to "Assass开发者_开发问答in's Creed: Brotherhood", this value is replaced by a new one. My project is developed with simplexml ( add, delete) but DOM is appreciated too. Thank you for your help!

My XML is

<Game type="adventure">
<TITLE>Assassin's Creed: Brotherhood</TITLE>
<PUBLISHER>Ubisoft</PUBLISHER>
</Game> 


With SimpleXML:

$games = simplexml_load_file('games.xml');
foreach ($games as $game) {
    if ($game->TITLE == "Assassin's Creed: Brotherhood") {
        $game->TITLE = "Kiss Me Kate";
    }
}
$games->asXML('games2.xml');
0

精彩评论

暂无评论...
验证码 换一张
取 消