Search code examples
phpxmlxmlreader

php XMLreader: editing element attributes & values


How I edit the attributes or values of an XML element?

the file:

<element>
  <apple type="fruit">red</apple>
</element>


XMLreader:

        $xml= new XMLReader();
        $xml->open($file);

        while($xml->read()){
            if($xml->nodeType == XMLReader::ELEMENT) {
                if($xml->getAttribute('type') == "fruit") {

                       //change attr. and values to:
                       //apple = cabbage, fruit = vegetable, red = white

                }
            }               
        }

Solution

  • Well, in order to edit XML, you must not use a reader class ;-)


    Instead, take a look at DOMDocument or SimpleXML.