|
xml_parse_into_structParse XML data into an array structure Description
int xml_parse_into_struct(
XMLParser $parser ,string $data ,array &$values ,array &$index = null )
This function parses an XML string into 2 parallel array structures, one
( Parameters
Return Values
xml_parse_into_struct returns 0 for failure and 1 for
success. This is not the same as Changelog
Examples
Below is an example that illustrates the internal structure of
the arrays being generated by the function. We use a simple
Example #1 xml_parse_into_struct example
When we run that code, the output will be: Index array Array ( [PARA] => Array ( [0] => 0 [1] => 2 ) [NOTE] => Array ( [0] => 1 ) ) Vals array Array ( [0] => Array ( [tag] => PARA [type] => open [level] => 1 ) [1] => Array ( [tag] => NOTE [type] => complete [level] => 2 [value] => simple note ) [2] => Array ( [tag] => PARA [type] => close [level] => 1 ) ) Event-driven parsing (based on the expat library) can get complicated when you have an XML document that is complex. This function does not produce a DOM style object, but it generates structures amenable of being traversed in a tree fashion. Thus, we can create objects representing the data in the XML file easily. Let's consider the following XML file representing a small database of aminoacids information: Example #2 moldb.xml - small database of molecular information <?xml version="1.0"?> <moldb> <molecule> <name>Alanine</name> <symbol>ala</symbol> <code>A</code> <type>hydrophobic</type> </molecule> <molecule> <name>Lysine</name> <symbol>lys</symbol> <code>K</code> <type>charged</type> </molecule> </moldb> Example #3 parsemoldb.php - parses moldb.xml into an array of molecular objects
** Database of AminoAcid objects: Array ( [0] => aminoacid Object ( [name] => Alanine [symbol] => ala [code] => A [type] => hydrophobic ) [1] => aminoacid Object ( [name] => Lysine [symbol] => lys [code] => K [type] => charged ) ) |