WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

PHP xml_parse_into_struct() function


Definition and usage

The xml_parse_into_struct () function parses XML data into an array.

This function parses the XML data into 2 arrays:

  • Value array-contains data from the parsed XML
  • Index array-contains a pointer to the position of the value in the Value array

If successful, the function returns 1. If it fails, 0 is returned.

Syntax

xml_parse_into_struct (parser, xml, value_arr, index_arr)

parameters description
parser Required. Specifies the XML parser to use.
xml Required. Specifies the XML data to be parsed
value_arr Required. Specifies the target array of XML data
index_arr Optional. Specifies the target array of index data.


Tips and comments

Note: The xml_parse_into_struct () function returns 1 if successful and 0 if it fails. This is different from TRUE and FALSE, so be careful when using operators such as ===.


Example

XML file

<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

PHP 代码

<?php
//invalid xml file
$xmlfile = 'test.xml';
$xmlparser = xml_parser_create();

//open a file and read data
$fp = fopen($xmlfile, 'r');
$xmldata = fread($fp, 4096);

xml_parse_into_struct($xmlparser,$xmldata,$values);

xml_parser_free($xmlparser);
print_r($values);
?>

The output of the above code is shown below:

Array
(
[0] => Array
(
[tag] => NOTE
[type] => open
[level] => 1
[value] =>
)
[1] => Array
(
[tag] => TO
[type] => complete
[level] => 2
[value] => Tove
)
[2] => Array
(
[tag] => NOTE
[value] =>
[type] => cdata
[level] => 1
)
[3] => Array
(
[tag] => FROM
[type] => complete
[level] => 2
[value] => Jani
)
[4] => Array
(
[tag] => NOTE
[value] =>
[type] => cdata
[level] => 1
)
[5] => Array
(
[tag] => HEADING
[type] => complete
[level] => 2
[value] => Reminder
)
[6] => Array
(
[tag] => NOTE
[value] =>
[type] => cdata
[level] => 1
)
[7] => Array
(
[tag] => BODY
[type] => complete
[level] => 2
[value] => Don't forget me this weekend!
)
[8] => Array
(
[tag] => NOTE
[value] =>
[type] => cdata
[level] => 1
)
[9] => Array
(
[tag] => NOTE
[type] => close
[level] => 1
)
)