How can XML data be manipulated using PHP?

XML data can be manipulated using PHP by utilizing the SimpleXML extension, which provides an easy way to interact with XML documents. This extension allows you to load XML data, traverse the XML structure, modify elements and attributes, and save the changes back to the XML file.

// Load XML data
$xml = simplexml_load_file('data.xml');

// Access and modify specific elements
$xml->book[0]->title = 'New Title';
$xml->book[1]->author = 'New Author';

// Add new elements
$newBook = $xml->addChild('book');
$newBook->addChild('title', 'Another Title');
$newBook->addChild('author', 'Another Author');

// Save changes back to the XML file
$xml->asXML('data.xml');