Home > Backend Development > PHP Tutorial > Detailed explanation of simple xml function usage in PHP

Detailed explanation of simple xml function usage in PHP

WBOY
Release: 2023-06-16 08:32:01
Original
985 people have browsed it

PHP is a popular server-side scripting language often used to develop dynamic websites. In website development, data in XML format is very common. PHP provides a series of simple and easy-to-use XML processing functions to facilitate us to operate XML data.

In this article, we will introduce the XML functions in PHP in detail, including operations such as creating XML documents, reading and parsing XML files, and using XPath to query XML data.

1. Create XML document

In PHP, we can use the DOMDocument class to create an XML document. For example, the following code can create an empty XML file:

$doc = new DOMDocument();
$doc->save('test.xml');
Copy after login

In the above code, we first instantiate a DOMDocument class and then save it as a test.xml file. This file does not contain any data, only a root node.

If we need to add more nodes and data to the XML file, we can use a series of methods of the DOMDocument class. For example, the following code can create an XML file containing multiple nodes:

// 创建一个根节点
$root = $doc->createElement('bookstore');
$doc->appendChild($root);

// 创建一个book节点,并添加到根节点中
$book1 = $doc->createElement('book');
$book1->setAttribute('category', 'web');
$root->appendChild($book1);

// 添加book节点的子节点
$title1 = $doc->createElement('title', 'Learning PHP');
$author1 = $doc->createElement('author', 'David Sklar');
$year1 = $doc->createElement('year', '2014');
$book1->appendChild($title1);
$book1->appendChild($author1);
$book1->appendChild($year1);

// 再创建一个book节点
$book2 = $doc->createElement('book');
$book2->setAttribute('category', 'web');
$root->appendChild($book2);

$title2 = $doc->createElement('title', 'PHP Programming');
$author2 = $doc->createElement('author', 'Kevin Tatroe');
$year2 = $doc->createElement('year', '2017');
$book2->appendChild($title2);
$book2->appendChild($author2);
$book2->appendChild($year2);

$doc->save('books.xml');
Copy after login

In the above code, we create a root node bookstore and add two book nodes to it, each book node contains The three child nodes are title, author and year. Finally save the entire document as books.xml file.

2. Reading and parsing XML files

In PHP, we can also use the DOMDocument class to read and parse XML files. For example, the following code can read the books.xml file and output the data:

$doc = new DOMDocument();
$doc->load('books.xml');

$root = $doc->documentElement;

$books = $root->getElementsByTagName('book');
foreach ($books as $book) {
    $title = $book->getElementsByTagName('title')->item(0)->nodeValue;
    $author = $book->getElementsByTagName('author')->item(0)->nodeValue;
    $year = $book->getElementsByTagName('year')->item(0)->nodeValue;

    echo $title . ' - ' . $author . ' - ' . $year . '<br>';
}
Copy after login

In the above code, we first instantiate a DOMDocument class and use the load method to read the books.xml file . Then get the root node bookstore of the document, get the book node from it, and loop through each book node to get the values ​​of the title, author, and year nodes, and output them to the browser.

3. Query XML data using XPath

XPath is a query language used to locate information in XML documents. In PHP, we can use DOMXPath class to perform XPath queries. For example, the following code can query all book nodes in the books.xml file whose category is web:

$doc = new DOMDocument();
$doc->load('books.xml');

$xpath = new DOMXPath($doc);
$books = $xpath->query('/bookstore/book[@category="web"]');

foreach ($books as $book) {
    $title = $book->getElementsByTagName('title')->item(0)->nodeValue;
    $author = $book->getElementsByTagName('author')->item(0)->nodeValue;
    $year = $book->getElementsByTagName('year')->item(0)->nodeValue;

    echo $title . ' - ' . $author . ' - ' . $year . '<br>';
}
Copy after login

In the above code, we first instantiate a DOMXPath class and use the query method to perform The result is all book nodes whose category is web. Then it also loops through the query results and outputs the data in the node to the browser.

Summary

Through the introduction of this article, we have learned about the simple and easy-to-use XML functions in PHP, including creating XML documents, reading and parsing XML files, and using XPath to query XML data. . These functions provide convenience and flexibility for us to process XML data in website development.

The above is the detailed content of Detailed explanation of simple xml function usage in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template