Skip to content
PHP Logo

What is a common method in PHP for extracting data from an XML file?

When working with XML files in PHP, a common method for extracting data is to use the SimpleXML extension. This extension provides an easy way to access and manipulate XML data by converting it into an object that can be traversed like any other PHP object. By using SimpleXML, you can easily extract specific elements or attributes from an XML file and use them in your PHP code.

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

// Access specific elements or attributes
$title = $xml->title;
$author = $xml->author;

// Loop through elements
foreach ($xml->children() as $child) {
    echo $child->getName() . ": " . $child . "<br>";
}