0

I need to change xml attributes using PHP (i.e. Name, Phone1, Phone2). The changed attribute values need to save to the file where the code was taken from. Could anyone set me on the right track?

    <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>
Student
  • 55
  • 5
  • Possible duplicate of [How do you parse and process HTML/XML in PHP?](https://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) – user3942918 Aug 02 '17 at 08:17

2 Answers2

1

You can use setAttribute

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

user10089632
  • 5,216
  • 1
  • 26
  • 34
0

You can simply use the PHP: XMLWriter for this: http://php.net/manual/de/function.xmlwriter-write-element.php

Also for reading XML-Files: http://php.net/manual/en/simplexml.examples-basic.php

Is it a remote XML or local on your webserver? - The thing is you can not edit remote files on the fly, I think. The best way is to fetch the old one, change the values while parsing the XML and save it again.

yfain
  • 509
  • 7
  • 23
  • its local on my webserver – Student Aug 02 '17 at 09:41
  • So you can fetch the XML, read it, change the values and save the edit on your local webserver. Take a look: https://stackoverflow.com/questions/2092172/edit-xml-with-simplexml – yfain Aug 02 '17 at 10:30