How can SimpleXML and XPath be effectively used in PHP to extract and manipulate data from XML files?

To extract and manipulate data from XML files in PHP, you can use SimpleXML to load the XML file and XPath to query specific elements or attributes within the XML structure. By combining these two tools, you can easily access and manipulate the data in the XML file.

// Load the XML file using SimpleXML
$xml = simplexml_load_file('data.xml');

// Use XPath to query specific elements or attributes
$results = $xml->xpath('//book[@category="fiction"]');

// Loop through the results and manipulate the data
foreach ($results as $result) {
    // Manipulate the data here
}