How can PHP be used to extract specific data from XML entries with the same name?

When extracting specific data from XML entries with the same name, you can use PHP's SimpleXMLElement class along with XPath to target the specific elements you want. By using XPath expressions, you can pinpoint the exact nodes you need even if they share the same name. This allows you to extract the desired data efficiently and accurately.

$xml = <<<XML
<root>
    <entry>
        <name>John</name>
        <age>25</age>
    </entry>
    <entry>
        <name>Jane</name>
        <age>30</age>
    </entry>
</root>
XML;

$entries = new SimpleXMLElement($xml);

// Extracting specific data from XML entries with the same name
foreach ($entries->xpath('//entry') as $entry) {
    $name = (string)$entry->name;
    $age = (int)$entry->age;

    echo "Name: $name, Age: $age" . PHP_EOL;
}