Student
Student

Reputation: 55

How to set xml attribute and save changes to file?

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>

Upvotes: 0

Views: 372

Answers (2)

yfain
yfain

Reputation: 496

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.

Upvotes: 0

user10089632
user10089632

Reputation: 5550

You can use setAttribute

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

Upvotes: 1

Related Questions