Current Location: Home> Latest Articles> How to use xml_parse with xml_set_character_data_handler

How to use xml_parse with xml_set_character_data_handler

M66 2025-04-26

When processing XML data, PHP provides a powerful set of XML parser extensions, where xml_parse and xml_set_character_data_handler are important tools for parsing XML text nodes. These two functions work together to effectively read and process text content in XML. This article will use a simple example to show how to use them to parse XML data.

1. Basic concepts

xml_parse

xml_parse() is the core function used to start parsing XML data. It receives a parser resource and an XML string as parameters, and each call parses part of it.

xml_set_character_data_handler

xml_set_character_data_handler() is used to set a callback function that will be triggered when encountering a text node in XML. That is, all plain text (CDATA) content between tags will be processed through this function.

2. Example: parse XML and extract text

Let's use an example to show how to use these two functions to extract text content in XML.

 <?php

// 1. Prepare XML String
$xmlData = <<<XML
<books>
    <book>
        <title>PHP Programming practice</title>
        <author>Zhang San</author>
    </book>
    <book>
        <title>In-depth understanding XML</title>
        <author>Li Si</author>
    </book>
</books>
XML;

// 2. Initialize a XML Parser
$parser = xml_parser_create();

// 3. Store extracted text content
$textContents = [];

// 4. Define a callback function that processes text
function handleCharacterData($parser, $data) {
    global $textContents;

    // Clean up blank characters
    $data = trim($data);
    if (!empty($data)) {
        $textContents[] = $data;
    }
}

// 5. Set character data processing function
xml_set_character_data_handler($parser, "handleCharacterData");

// 6. Analysis XML data
if (!xml_parse($parser, $xmlData, true)) {
    die("XML mistake: " . xml_error_string(xml_get_error_code($parser)));
}

// 7. 释放Parser资源
xml_parser_free($parser);

// 8. Output the extracted text content
echo "The extracted text content is as follows:<br>";
foreach ($textContents as $text) {
    echo htmlspecialchars($text) . "<br>";
}

?>

3. Output result

After running the above PHP script, the following output is:

 The extracted text content is as follows:
PHP Programming practice
Zhang San
In-depth understanding XML
Li Si

IV. Application scenarios

This parsing method is especially suitable for processing small XML data, or when you want to further process text content between tags. For example:

  • Crawl news titles and content in RSS feeds;

  • Parses custom XML responses returned from https://api.m66.net/data.xml ;

  • Extract the text in the configuration file or data file, etc.

5. Things to note

  • The parser created with xml_parser_create() needs to release the resources through xml_parser_free() after use;

  • Text content processing functions often need to filter whitespace characters to avoid interference;

  • PHP's XML parser is event-driven and requires registration of multiple processing functions (such as xml_set_element_handler ) to handle different XML parts.

Combining xml_parse and xml_set_character_data_handler can handle text content in XML very flexibly. Whether it is simple string extraction or complex logical processing, it can be achieved through appropriate callback design.

I hope this article can help you better understand how XML parses in PHP! If you need to further combine data with database or web page display, you can continue to expand the code structure to achieve richer application functions.