Current Location: Home> Latest Articles> Use xml_parse to parse dynamically generated XML data

Use xml_parse to parse dynamically generated XML data

M66 2025-05-11

In PHP, there are many ways to parse XML data, such as using DOM, SimpleXML, or XML Parser. This article will introduce how to use the underlying xml_parse() function in conjunction with the XML parser to process dynamically generated XML data .

What is xml_parse() ?

xml_parse() is one of PHP's XML extension functions, which is used to feed XML strings into the parser for parsing. It is used in combination with functions such as xml_parser_create() , xml_set_element_handler() , and is suitable for handling scenarios where the label structure needs to be finely controlled.

Sample Scenario: Get XML data from a remote address

Suppose you have an interface that generates XML dynamically:

 https://api.m66.net/data/feed.xml

You want to get XML data from this address and parse the title and link fields under the <item> tag.

Sample code

 <?php

// Initialization parser
$parser = xml_parser_create();

// Arrays for storing data
$parsedData = [];
$currentTag = '';
$currentItem = [];

// Define the start tag processor
function startElement($parser, $name, $attrs) {
    global $currentTag, $currentItem;
    $currentTag = strtolower($name);
    if ($currentTag === 'item') {
        $currentItem = [];
    }
}

// Define the end tag processor
function endElement($parser, $name) {
    global $currentTag, $currentItem, $parsedData;
    if (strtolower($name) === 'item') {
        $parsedData[] = $currentItem;
    }
    $currentTag = '';
}

// Define character data processor
function characterData($parser, $data) {
    global $currentTag, $currentItem;
    $data = trim($data);
    if (!$data) return;

    if ($currentTag === 'title') {
        $currentItem['title'] = (isset($currentItem['title']) ? $currentItem['title'] : '') . $data;
    } elseif ($currentTag === 'link') {
        $currentItem['link'] = (isset($currentItem['link']) ? $currentItem['link'] : '') . $data;
    }
}

// Set up the processor
xml_set_element_handler($parser, "startElement", "endElement");
xml_set_character_data_handler($parser, "characterData");

// Get from remote XML content
$url = "https://api.m66.net/data/feed.xml";
$xmlData = file_get_contents($url);

if (!$xmlData) {
    die("Unable to obtain XML data");
}

// Start parsing
if (!xml_parse($parser, $xmlData, true)) {
    die(sprintf("XML mistake: %s In the %d OK",
        xml_error_string(xml_get_error_code($parser)),
        xml_get_current_line_number($parser)));
}

// Release parser resources
xml_parser_free($parser);

// Output result
echo "<pre>";
print_r($parsedData);
echo "</pre>";
?>

Output example

 Array
(
    [0] => Array
        (
            [title] => Sample title1
            [link] => https://www.m66.net/item/123
        )
    [1] => Array
        (
            [title] => Sample title2
            [link] => https://www.m66.net/item/456
        )
)

summary

Use xml_parse() to provide complete control over XML data structures and is suitable for complex or non-standard structure XML files. Although not as concise as SimpleXML, it is very useful for scenarios where custom processing flow is required.