Click to See Complete Forum and Search --> : Replacing attributes of an XML node


ropotan
11-30-2009, 11:13 AM
Hello, i'm pretty new to XML development, and i need some help with a pretty basic task.
I have this simple XML file, which has a basic structure like:
<players>
<name="John" x="10" y="10"></name>
</players>

and i'm trying to build a sort of PHP parser which receives an x and y value for a specific name and changes the attributes of that specific name. i googled and found some options like replacing the whole node, with remove and add in SimpleXML or DOM. i was just wondering if there is another more elegant way to change the x and y attribute or is this the best way to do it?

Thank you!

Webnerd
12-04-2009, 10:27 AM
$xml = <<<EOF
<players>
<name="John" x="10" y="10"></name>
</players>
EOF;

$x = 20;
$y = 30;

$xml = preg_replace("/x=\"\d+\" y=\"\d+\"","x=\"{$x}\" y=\"{$y}\"/",$xml);



You can always use DOM if your attributes change position.