Backend Development 5 min read

Using PHP Built‑in Functions to Read, Modify, Create, and Parse XML Data

This tutorial explains how to use PHP's SimpleXML and DOMDocument functions to load, read, modify, create, and parse XML files, providing clear code examples for each operation and demonstrating how to output or save the resulting XML.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP Built‑in Functions to Read, Modify, Create, and Parse XML Data

XML (Extensible Markup Language) is a widely used data format designed for cross‑platform data transmission and storage. In web applications, handling XML data is a common task. This article introduces how to use PHP's built‑in XML functions to process XML data.

Reading XML Data

PHP provides a set of functions for reading XML data, the most common being simplexml_load_file , which loads an XML file into a SimpleXML object for easy manipulation.

<code>$xml = simplexml_load_file("books.xml");</code>

After loading the XML file, you can use the SimpleXML object's methods to access elements, for example:

<code>// Get title and author of the first book element
$title = $xml->book[0]->title;
$author = $xml->book[0]->author;
// Output result
echo $title . " by " . $author;</code>

This code outputs the title and author of the first book.

Modifying XML Data

PHP can easily modify an XML document. The following code changes the title of the first book:

<code>// Change the first book's title to "New Title"
$xml->book[0]->title = "New Title";
// Save the modified XML back to the file
$xml->asXML("books.xml");</code>

The code updates the first book's title to "New Title" and saves the changes to the file.

Creating XML Data

In addition to reading and modifying XML, PHP can create new XML documents. The example below creates a simple XML structure with a root element and a book child element:

<code>// Create a new XML document
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><root></root>');
// Add a new book element
$book = $xml->addChild("book");
// Add child elements
$book->addChild("title", "PHP Programming");
$book->addChild("author", "John Smith");
$book->addChild("price", "19.99");
// Output the XML document
echo $xml->asXML();</code>

This code creates a root element, adds a book with title, author, and price, and outputs the formatted XML.

Parsing XML Data

Parsing XML is another common task. PHP offers the DOMDocument class for this purpose. The following example demonstrates how to load an XML file and iterate over all book elements to retrieve their titles and authors:

<code>// Create a DOMDocument object
$doc = new DOMDocument();
// Load the XML file
$doc->load("books.xml");
// Get all book elements
$books = $doc->getElementsByTagName("book");
// Iterate over each book
foreach ($books as $book) {
    // Get title value
    $title = $book->getElementsByTagName("title")->item(0)->nodeValue;
    // Get author value
    $author = $book->getElementsByTagName("author")->item(0)->nodeValue;
    // Output title and author
    echo $title . " by " . $author;
}
</code>

This code loads books.xml , uses DOMDocument to traverse all book elements, and retrieves the title and author values via getElementsByTagName and the nodeValue property.

PHP provides many functions and classes for reading, modifying, creating, and parsing XML data. Whether you are handling simple XML files or interacting with SOAP/REST web services, PHP offers a quick and convenient way to work with XML.

backend developmentPHPXMLDOMDocumentSimpleXML
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.