Skip to content

xml_parse_into_struct()

The xml_parse_into_struct() function is a PHP built-in function that parses XML data into a multidimensional array. It belongs to the XML Parser extension and provides a SAX-like interface for converting XML into a structured array that can be easily manipulated. Ensure the xml extension is enabled in your php.ini configuration before using this function.

The function is useful when you need to extract data from an XML file, for example, to convert XML data into another format or to analyze the structure of an XML file.

Syntax

The syntax of the xml_parse_into_struct() function is as follows:


php
xml_parse_into_struct($parser, $data, &$values, &$index)

Where $parser is the XML parser resource returned by xml_parser_create(), $data is the XML data to be parsed, $values is an array that will contain the parsed data, and $index is an array that will contain the index values of the parsed data.

Usage Examples

Let's take a look at some practical examples of using xml_parse_into_struct() in PHP.

Example 1: Parsing XML Data into a Structured Array

Suppose you have an XML file data.xml that you want to parse into a structured array. You can use the xml_parse_into_struct() function to parse the XML data, like this:


php
$xml_parser = xml_parser_create();
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 0);
$xml_data = file_get_contents("data.xml");
$values = array();
$index = array();
if (!xml_parse_into_struct($xml_parser, $xml_data, $values, $index)) {
  $error_message = xml_error_string(xml_get_error_code($xml_parser));
  $error_line = xml_get_current_line_number($xml_parser);
  echo "XML Parsing Error: $error_message at line $error_line";
}
xml_parser_free($xml_parser);

This code creates an XML parser using xml_parser_create() and disables case folding. It reads the XML file and stores the contents in $xml_data. It initializes empty $values and $index arrays, then calls xml_parse_into_struct() to populate them. If parsing fails, it retrieves the error code and message, and outputs a detailed error string with the line number. Finally, it frees the parser memory with xml_parser_free().

Example 2: Analyzing the Structure of an XML File

Suppose you want to analyze the structure of data.xml by inspecting the parsed elements. After parsing, the $values array contains associative arrays for each XML element with keys like tag, type, value, and level. You can iterate over this array to examine the document structure:

php
$xml_parser = xml_parser_create();
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 0);
$xml_data = file_get_contents("data.xml");
$values = array();
$index = array();
if (!xml_parse_into_struct($xml_parser, $xml_data, $values, $index)) {
  $error_message = xml_error_string(xml_get_error_code($xml_parser));
  $error_line = xml_get_current_line_number($xml_parser);
  echo "XML Parsing Error: $error_message at line $error_line";
}
xml_parser_free($xml_parser);

foreach ($values as $value) {
  if ($value["type"] == "open") {
    echo "Start element: " . $value["tag"] . "<br/>";
  } else if ($value["type"] == "close") {
    echo "End element: " . $value["tag"] . "<br/>";
  }
}

This code reuses the standard parsing setup. After parsing, it iterates over the $values array using a foreach loop. It checks the type key for each element: if it is "open", it prints the start tag; if it is "close", it prints the end tag. This approach provides a straightforward way to analyze the hierarchical structure of an XML file using the parsed data.

Conclusion

In this article, we've discussed PHP's xml_parse_into_struct() function and how it can be used to parse XML data into a structured array. We've explained what the function does, its syntax, and provided examples of how it can be used in practical scenarios. By following these examples, you can easily parse XML data, extract data from XML files, transform XML data into another format, or analyze the structure of an XML file in your PHP applications.

Practice

What does the PHP function xml_parse_into_struct() do?

Do you find this helpful?

Dual-run preview — compare with live Symfony routes.