PHP xml_parse_into_struct() Function
The PHP xml_parse_into_struct() function parses an XML data into an array structure. It parses an XML string into 2 parallel array structures:
- Value array - containing the data from the parsed XML
- Index array - containing pointers to the location of the values in the Value array
The last two parameters of this function must be passed by reference.
Syntax
xml_parse_into_struct(parser, data, values, index)
Parameters
parser |
Required. Specify a reference to the XML parser to use. |
data |
Required. Specify a string containing the XML data to parse. |
values |
Required. Specify an array containing the values of the XML data. |
index |
Optional. Specify an array containing pointers to the location of the appropriate values in the values. |
Return Value
Returns 1 on success or 0 on failure. This is not the same as false and true.
Example: xml_parse_into_struct() example
The example shows the usage of xml_parse_into_struct function.
<?php //create an XML parser $parser = xml_parser_create(); //a simple XML data to parse $xmldata = "<p><note>Hello World!</note></p>"; //parsing XML data into an array xml_parse_into_struct($parser, $xmldata, $vals, $index); //free XML parser xml_parser_free($parser); echo "Index array\n"; print_r($index); echo "\nVals array\n"; print_r($vals); ?>
The output of the above code will be:
Index array Array ( [P] => Array ( [0] => 0 [1] => 2 ) [NOTE] => Array ( [0] => 1 ) ) Vals array Array ( [0] => Array ( [tag] => P [type] => open [level] => 1 ) [1] => Array ( [tag] => NOTE [type] => complete [level] => 2 [value] => Hello World! ) [2] => Array ( [tag] => P [type] => close [level] => 1 ) )
Example: parsing a XML file
Lets assume that we have a file called test.xml. This file contains following content:
<?xml version="1.0" encoding="UTF-8"?> <userlist> <user> <username>John123</username> <name>John Smith</name> <phone>+1-8054098000</phone> <address>Brooklyn, New York, USA</address> </user> </userlist>
In the example below, an XML parser is created. Then the data of this XML file is parsed using xml_parse_into_struct() function.
<?php //create an XML parser $parser=xml_parser_create(); //opening the xml file $fp = fopen("test.xml", "r"); //the XML data to parse $xmldata = fread($fp,4096); //parsing XML data into an array xml_parse_into_struct($parser, $xmldata, $vals, $index); //free XML parser xml_parser_free($parser); fclose($fp); echo "Vals array\n"; print_r($vals); ?>
The output of the above code will be:
Vals array Array ( [0] => Array ( [tag] => USERLIST [type] => open [level] => 1 [value] => ) [1] => Array ( [tag] => USER [type] => open [level] => 2 [value] => ) [2] => Array ( [tag] => USERNAME [type] => complete [level] => 3 [value] => John123 ) [3] => Array ( [tag] => USER [value] => [type] => cdata [level] => 2 ) [4] => Array ( [tag] => NAME [type] => complete [level] => 3 [value] => John Smith ) [5] => Array ( [tag] => USER [value] => [type] => cdata [level] => 2 ) [6] => Array ( [tag] => PHONE [type] => complete [level] => 3 [value] => +1-8054098000 ) [7] => Array ( [tag] => USER [value] => [type] => cdata [level] => 2 ) [8] => Array ( [tag] => ADDRESS [type] => complete [level] => 3 [value] => Brooklyn, New York, USA ) [9] => Array ( [tag] => USER [value] => [type] => cdata [level] => 2 ) [10] => Array ( [tag] => USER [type] => close [level] => 2 ) [11] => Array ( [tag] => USERLIST [value] => [type] => cdata [level] => 1 ) [12] => Array ( [tag] => USERLIST [type] => close [level] => 1 ) )
❮ PHP XML Parser Reference