PHP Simple XML

please click here for more wordpress cource

PHP SimpleXML is a built-in PHP extension that allows you to easily access and manipulate XML data. It provides a simple API for parsing and manipulating XML documents, allowing you to extract data from an XML file, create new XML documents, and modify existing XML documents.

Here’s an example of how to use SimpleXML to parse an XML file and access its elements:

$xml = simplexml_load_file('example.xml');

// Access elements
echo $xml->title;
echo $xml->author->name;

// Loop through elements
foreach ($xml->books->book as $book) {
    echo $book->title;
}

// Modify elements
$xml->title = 'New Title';
$xml->author->name = 'New Author';

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

// Save changes
$xml->asXML('example.xml');

In this example, we load an XML file using simplexml_load_file(), access its elements using object notation, loop through elements using a foreach loop, modify elements using assignment, and add new elements using addChild(). Finally, we save the modified XML file using asXML().

You may also like...

Popular Posts

Leave a Reply

Your email address will not be published. Required fields are marked *