Current Location: Home> Latest Articles> How to parse XML data through xml_parse and domxml function library

How to parse XML data through xml_parse and domxml function library

M66 2025-05-12

In modern web application development, XML (extensible markup language) is one of the data exchange formats and is still widely used in many scenarios. PHP provides a variety of function libraries for processing XML, among which xml_parse and domxml are two common parsing methods, each suitable for different needs. This article will introduce these two methods separately and provide practical code examples to help you efficiently master the parsing skills of XML data.

1. Use xml_parse for event-based parsing

xml_parse is an XML parser function in PHP and belongs to an event-based parsing method (also known as SAX parsing). It triggers a specific callback function during the process of reading XML data, which is suitable for handling large XML files because it does not require the entire document to be loaded into memory.

Sample code:

 <?php
$xml_data = <<<XML
<books>
    <book>
        <title>PHP Programming practice</title>
        <author>Zhang San</author>
    </book>
    <book>
        <title>XML and Web Serve</title>
        <author>Li Si</author>
    </book>
</books>
XML;

$parser = xml_parser_create("UTF-8");

function startElement($parser, $name, $attrs) {
    echo "Start Element:$name\n";
}

function endElement($parser, $name) {
    echo "Ending Element:$name\n";
}

function characterData($parser, $data) {
    $data = trim($data);
    if (!empty($data)) {
        echo "data:$data\n";
    }
}

xml_set_element_handler($parser, "startElement", "endElement");
xml_set_character_data_handler($parser, "characterData");

if (!xml_parse($parser, $xml_data, true)) {
    die("XML Parsing error:" . xml_error_string(xml_get_error_code($parser)));
}

xml_parser_free($parser);
?>

Output result:

 Start Element:BOOKS
Start Element:BOOK
Start Element:TITLE
data:PHP Programming practice
Ending Element:TITLE
Start Element:AUTHOR
data:Zhang San
Ending Element:AUTHOR
Ending Element:BOOK
Start Element:BOOK
Start Element:TITLE
data:XML and Web Serve
Ending Element:TITLE
Start Element:AUTHOR
data:Li Si
Ending Element:AUTHOR
Ending Element:BOOK
Ending Element:BOOKS

Advantages and applicable scenarios

  • Advantages : fast parsing speed and low memory usage.

  • Applicable scenarios : Large XML files, only need to be read without modifying XML content.

2. Use domxml to perform analysis based on document object model (DOM)

domxml provides a more structured XML processing method that allows you to manipulate XML data like HTML DOM by loading the entire XML document into a DOM tree.

Note : Since PHP5, domxml has been replaced with DOM extensions (i.e. DOMDocument class), and this article adopts the latter example.

Sample code:

 <?php
$xml_string = <<<XML
<books>
    <book>
        <title>PHP Programming practice</title>
        <author>Zhang San</author>
        <link>https://www.m66.net/book/php</link>
    </book>
    <book>
        <title>XML and Web Serve</title>
        <author>Li Si</author>
        <link>https://www.m66.net/book/xml</link>
    </book>
</books>
XML;

$doc = new DOMDocument();
$doc->loadXML($xml_string);

$books = $doc->getElementsByTagName("book");

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

    echo "Book title:$title\n";
    echo "author:$author\n";
    echo "Link:$link\n\n";
}
?>

Output result:

 Book title:PHP Programming practice
author:Zhang San
Link:https://www.m66.net/book/php

Book title:XML and Web Serve
author:Li Si
Link:https://www.m66.net/book/xml

Advantages and applicable scenarios

  • Advantages : Clear structure and easy to operate, suitable for reading, modifying and generating XML.

  • Applicable scenarios : small to medium-sized XML files, structure modification or new documents need to be created.