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

时间:2017-08-02 08:17:08

标签: php xml attributes setattribute

我需要使用PHP更改xml属性(即Name,Phone1,Phone2)。更改的属性值需要保存到取代码的文件中。 谁能让我走上正轨?

    <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>

2 个答案:

答案 0 :(得分:0)

您可以使用setAttribute

  

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

答案 1 :(得分:0)

您可以简单地使用PHP:XMLWriter: http://php.net/manual/de/function.xmlwriter-write-element.php

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

您的网络服务器上是远程XML还是本地? - 我认为,你无法动态编辑远程文件。 最好的方法是获取旧的,在解析XML时更改值并再次保存。