如何设置 xml 属性并将更改保存到文件?

How to set xml attribute and save changes to file?

我需要使用 PHP 更改 xml 属性(即姓名、电话 1、电话 2)。更改后的属性值需要保存到从中获取代码的文件中。 谁能让我走上正轨?

    <PhoneBook>
<Title>TRSA</Title>
<Menu Name="Informational">
<Unit Name="Name Surname" Phone1="476" Phone2="8 888 88 888"         
Phone3="62811" default_photo="Resource:"/>
<Unit Name="John Johnson" Phone1="412" Phone2="" Phone3="62812"         
default_photo="Resource:"/>
<Unit Name="Carl Johnson" Phone1="481" Phone2="8 888 88 888" 
Phone3="22222" default_photo="Resource:"/>
<Unit Name="Carl Johnson" Phone1="481" Phone2="" Phone3="22222" 
default_photo="Resource:"/>
<Unit Name="Carl Johnson" Phone1="427" Phone2="" Phone3="22222" 
default_photo="Resource:"/>
<Unit Name="Carl Johnson" Phone1="414" Phone2="" Phone3="22222" 
default_photo="Resource:"/>
<Unit Name="Carl Johnson" Phone1="414" Phone2="8 888 88 888" 
Phone3="22222" default_photo="Resource:"/>
<Unit Name="Carl Johnson" Phone1="410" Phone2="8 888 88 888" 
Phone3="22222" default_photo="Resource:"/>
</Menu>
<Menu Name="Administration">
<Unit Name="Carl Johnson" Phone1="413" Phone2="8 888 88 888" 
Phone3="22222" default_photo="Resource:"/>
<Unit Name="Carl Johnson" Phone1="450" Phone2="8 888 88 888" 
Phone3="22222" default_photo="Resource:"/>
<Unit Name="Carl Johnson" Phone1="344" Phone2="8 888 88 888" 
Phone3="22222" default_photo="Resource:"/>
<Unit Name="Carl Johnson" Phone1="484" Phone2="" Phone3="52110" 
default_photo="Resource:"/>
<Unit Name="Carl Johnson" Phone1="465" Phone2="8 888 88 888" 
Phone3="22222" default_photo="Resource:"/>
<Unit Name="Carl Johnson " Phone1="522" Phone2="8 888 88 888" 
Phone3="22222" default_photo="Resource:"/>
</Menu>
</PhoneBook>

您可以使用setAttribute

public DOMAttr DOMElement::setAttribute ( string $name , string $value )

您只需使用 PHP: XMLWriter 即可: http://php.net/manual/de/function.xmlwriter-write-element.php

也用于阅读 XML-文件: http://php.net/manual/en/simplexml.examples-basic.php

它是远程 XML 还是本地网络服务器? - 问题是你不能即时编辑远程文件,我想。 最好的方法是获取旧的,在解析 XML 时更改值并再次保存。