<HTML ><HEAD ><TITLE >xml_parse_into_struct</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Manual" HREF="index.html"><LINK REL="UP" TITLE="XML parser functions" HREF="ref.xml.html"><LINK REL="PREVIOUS" TITLE="xml_get_error_code" HREF="function.xml-get-error-code.html"><LINK REL="NEXT" TITLE="xml_parse" HREF="function.xml-parse.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.xml-get-error-code.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.xml-parse.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.xml-parse-into-struct" ></A >xml_parse_into_struct</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN95911" ></A ><P > (PHP 3>= 3.0.8, PHP 4 )</P >xml_parse_into_struct -- Parse XML data into an array structure</DIV ><DIV CLASS="refsect1" ><A NAME="AEN95914" ></A ><H2 >Description</H2 >int <B CLASS="methodname" >xml_parse_into_struct</B > ( resource parser, string data, array &values [, array &index])<BR ></BR ><P > This function parses an XML file into 2 parallel array structures, one (<TT CLASS="parameter" ><I >index</I ></TT >) containing pointers to the location of the appropriate values in the <TT CLASS="parameter" ><I >values</I ></TT > array. These last two parameters must be passed by reference. </P ><P > Below is an example that illustrates the internal structure of the arrays being generated by the function. We use a simple <TT CLASS="literal" >note</TT > tag embeded inside a <TT CLASS="literal" >para</TT > tag, and then we parse this an print out the structures generated: <DIV CLASS="informalexample" ><A NAME="AEN95937" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >$simple = "<para><note>simple note</note></para>"; $p = xml_parser_create(); xml_parse_into_struct($p,$simple,$vals,$index); xml_parser_free($p); echo "Index array\n"; print_r($index); echo "\nVals array\n"; print_r($vals);</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > When we run that code, the output will be: <DIV CLASS="informalexample" ><A NAME="AEN95939" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >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 ) )</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P > 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 transversed 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: <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN95942" ></A ><P ><B >Example 1. moldb.xml - small database of molecular information</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="xml" ><?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></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > And some code to parse the document and generate the appropriate objects: <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN95945" ></A ><P ><B >Example 2. parsemoldb.php - parses moldb.xml into and array of molecular objects </B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php class AminoAcid { var $name; // aa name var $symbol; // three letter symbol var $code; // one letter code var $type; // hydrophobic, charged or neutral function AminoAcid ($aa) { foreach ($aa as $k=>$v) $this->$k = $aa[$k]; } } function readDatabase($filename) { // read the xml database of aminoacids $data = implode("",file($filename)); $parser = xml_parser_create(); xml_parser_set_option($parser,XML_OPTION_CASE_FOLDING,0); xml_parser_set_option($parser,XML_OPTION_SKIP_WHITE,1); xml_parse_into_struct($parser,$data,$values,$tags); xml_parser_free($parser); // loop through the structures foreach ($tags as $key=>$val) { if ($key == "molecule") { $molranges = $val; // each contiguous pair of array entries are the // lower and upper range for each molecule definition for ($i=0; $i < count($molranges); $i+=2) { $offset = $molranges[$i] + 1; $len = $molranges[$i + 1] - $offset; $tdb[] = parseMol(array_slice($values, $offset, $len)); } } else { continue; } } return $tdb; } function parseMol($mvalues) { for ($i=0; $i < count($mvalues); $i++) $mol[$mvalues[$i]["tag"]] = $mvalues[$i]["value"]; return new AminoAcid($mol); } $db = readDatabase("moldb.xml"); echo "** Database of AminoAcid objects:\n"; print_r($db); ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > After executing <TT CLASS="filename" >parsemoldb.php</TT >, the variable <TT CLASS="varname" >$db</TT > contains an array of <B CLASS="classname" >AminoAcid</B > objects, and the output of the script confirms that: <DIV CLASS="informalexample" ><A NAME="AEN95951" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >** 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 ) )</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.xml-get-error-code.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.xml-parse.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >xml_get_error_code</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.xml.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >xml_parse</TD ></TR ></TABLE ></DIV ></BODY ></HTML >