Search w3schools.com:

SHARE THIS PAGE

PHP xml_parse_into_struct() Function


PHP XML Reference Complete PHP XML Reference

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 - containing the data from the parsed XML
  • Index array - containing pointers to the location of the values in the Value array

This function returns 1 on success, or 0 on failure.

Syntax

xml_parse_into_struct(parser,xml,value_arr,index_arr)

Parameter Description
parser Required. Specifies XML parser to use
xml Required. Specifies XML data to parse
value_arr Required. Specifies the target array for the XML data
index_arr Optional. Specifies the target array for index data


Tips and Notes

Note: The xml_parse_into_struct() function returns 1 for success and 0 for failure.  This is not the same as TRUE and FALSE.


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 Code

<?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 code above will be:

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
  )
)


PHP XML Reference Complete PHP XML Reference

W3Schools Certification

W3Schools' Online Certification

The perfect solution for professionals who need to balance work, family, and career building.

More than 10 000 certificates already issued!

Get Your Certificate »

The HTML Certificate documents your knowledge of HTML.

The HTML5 Certificate documents your knowledge of advanced HTML5.

The CSS Certificate documents your knowledge of advanced CSS.

The JavaScript Certificate documents your knowledge of JavaScript and HTML DOM.

The jQuery Certificate documents your knowledge of jQuery.

The XML Certificate documents your knowledge of XML, XML DOM and XSLT.

The ASP Certificate documents your knowledge of ASP, SQL, and ADO.

The PHP Certificate documents your knowledge of PHP and SQL (MySQL).

Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]