<Root>
...
    <A CHANGE_THIS="Dont_ChengeME">
        <B></B>  
    </A>
...
</Root>

trying to change the attribute name of element A to something of my choice, using php's simpleXML API. here's what i've done:

$xml = simplexml_import_dom($xmldom);
    $query = "root/A[@]";
    $result = $xml->xpath($query);  
       if(!empty($result))
         $result['CHANGE_THIS'] = "Blalalalal";       //i believe this is where am doing it wrongly

All attempts failed. any idea ?

Member Avatar for diafol
$xml = simplexml_load_file("somefile.xml");
$xml->A[0]->attributes()->CHANGE_THIS = "Yes Sir!";
$xml->asXml('xml.xml');    

No need for xpath I don't think. I'm assuming the tag you want to target is the first or only of its type. If there are many and it's in the middle or end, just loop over each $xml->A until you get the attribute value CHANGE_THIS or even check the content of attribute before making a replacement.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.