Home > Backend Development > PHP Tutorial > Working with XML in PHP: How to parse XML and create XML documents

Working with XML in PHP: How to parse XML and create XML documents

PHPz
Release: 2023-06-19 17:06:01
Original
1433 people have browsed it

XML (Extensible Markup Language) is a common data format widely used in web development. In PHP, the use of XML is very common, so learning how to parse XML and create XML documents is very important for PHP developers.

This article will introduce you to the basic methods of parsing XML and creating XML documents in PHP.

  1. Parsing XML

There are two ways to parse XML: DOM-based and SAX-based. DOM (Document Object Model) represents the entire XML document as a tree and is used to operate on the entire document. SAX (Simple API for XML) is a streaming parsing method that reads XML documents node by node in sequence.

In this article, we will focus on DOM-based parsing methods.

First, we need to use the DOMDocument class in PHP to load the XML file:

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

This will create an XML document using the DOMDocument class and load the XML file "example.xml".

Next, we can use the getElementsByTagName method to get the specified XML element:

$books = $doc->getElementsByTagName('book');
Copy after login

This will get all the book elements and store them in the $books variable. We can get the sub-elements and sub-element values ​​of each book element by traversing the $books array:

foreach ($books as $book) {
    $title = $book->getElementsByTagName('title')->item(0)->nodeValue;
    $author = $book->getElementsByTagName('author')->item(0)->nodeValue;
    $publisher = $book->getElementsByTagName('publisher')->item(0)->nodeValue;
    echo "$title - $author - $publisher
";
}
Copy after login

This will traverse all book elements and obtain the title, author and publisher sub-elements in each book element. values ​​and output them.

  1. Create XML document

In PHP, we can use the DOMDocument class to create an XML document and add elements, attributes and text nodes.

First, we need to create an instance of the DOMDocument class:

$doc = new DOMDocument();
Copy after login

Next, we can use the createElement method to create an element and the appendChild method to add it to the DOM document:

$book = $doc->createElement('book');
$doc->appendChild($book);
Copy after login

This will create an element named "book" and add it to the DOM document.

We can use the setAttribute method to add attributes to the element:

$book->setAttribute('id', '1');
Copy after login

This will add an attribute named "id" with a value of "1" to the book element.

We can also create a text node using the createTextNode method and add it to the element using the appendChild method:

$title = $doc->createElement('title');
$book->appendChild($title);
$title_text = $doc->createTextNode('PHP入门教程');
$title->appendChild($title_text);
Copy after login

This will create a title child element for the book element and add it named " Text node for "Introduction to PHP Tutorial".

Finally, we can save the DOM document as an XML file using the save method:

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

This will save the DOM document as a file named "example.xml".

Conclusion

In PHP, XML processing is very common. We can use DOM-based parsing to obtain elements and data in XML documents, and use the DOMDocument class to create XML documents, add elements, attributes, and text nodes. These operations can bring great help to our web development.

The above is the detailed content of Working with XML in PHP: How to parse XML and create XML documents. 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