Initial parsing of a XML file
<?php
$xml_file = "/path/to/file.xml";
$xml_file_string = file_get_contents($xml_file);
$xml_parser = xml_parser_create();
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($xml_parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($xml_parser, $xml_file_string, $a_xml, $a_index);
xml_parser_free($xml_parser);
?>
$xml_file = "/path/to/file.xml";
$xml_file_string = file_get_contents($xml_file);
$xml_parser = xml_parser_create();
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($xml_parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($xml_parser, $xml_file_string, $a_xml, $a_index);
xml_parser_free($xml_parser);
?>
Once this code has been executed, the XML file will have been entered into the $a_xml array. You can view it's contents with print_r($a_xml);. It basically puts every node of the XML into an array slot. Each array slot contains an array that has the tag name, value of the tag, type of tag (close, open or complete) and maybe the attributes -- I forget where they are stored. Below are examples of a (close, open and complete) tag.
<xml_document> <!-- type=open -->
<car> <!-- type=open -->
<color> <!-- type=open -->
blue
</color> <!-- type=close -->
<condition /> <!-- type=complete -->
</car> <!-- type=close -->
</xml_document> <!-- type=close -->
<car> <!-- type=open -->
<color> <!-- type=open -->
blue
</color> <!-- type=close -->
<condition /> <!-- type=complete -->
</car> <!-- type=close -->
</xml_document> <!-- type=close -->
I've put this to use in examples below.
[Click to add or edit comments])
Please prepend comments below including a date