What are common issues when trying to extract data from XML using PHP?

Common issues when trying to extract data from XML using PHP include incorrectly formatted XML, missing or incorrect XPath queries, and errors in handling namespaces. To solve these issues, ensure that the XML is valid, double-check the XPath queries for accuracy, and properly handle namespaces if they are present in the XML document.

<?php
// Load the XML file
$xml = simplexml_load_file('data.xml');

// Check if the XML is loaded successfully
if ($xml === false) {
    die('Error loading XML file');
}

// Define the XPath query to extract data
$xpath = '/root/element/subelement';

// Register any namespaces if needed
$xml->registerXPathNamespace('ns', 'http://example.com');

// Extract data using XPath query
$result = $xml->xpath($xpath);

// Loop through the result
foreach ($result as $node) {
    // Process the extracted data
    echo $node->asXML();
}
?>