Documentation and examples of xml_parse_into_struct

git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@30694 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
Jesus M. Castagnetto 2000-08-21 08:01:40 +00:00
parent 608459c3e4
commit 88a84a2461

View file

@ -1621,6 +1621,220 @@ $xml_parser->parse("<A ID=\"hallo\">PHP</A>");
</refsect1>
</refentry>
<refentry id="function.xml-parse-into-struct">
<refnamediv>
<refname>xml_parse_into_struct</refname>
<refpurpose>Parse XML data into an array structure</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<funcsynopsis>
<funcprototype>
<funcdef>int <function>xml_parse_into_struct</function></funcdef>
<paramdef>int <parameter>parser</parameter></paramdef>
<paramdef>string <parameter>data</parameter></paramdef>
<paramdef>array <parameter>&amp;values</parameter></paramdef>
<paramdef>array <parameter>&amp;index</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<para>
This function parses an XML file into 2 parallel array
structures, one (<parameter>index</parameter>) containing pointers
to the location of the appropriate values in the
<parameter>values</parameter> array. These last two parameters
must be passed by reference.
</para>
<para>
Below is an example that illustrates the internal structure of
the arrays being generated by the function. We use a simple
<literal>note</literal> tag embeded inside a
<literal>para</literal> tag, and then we parse this an print out
the structures generated:
<informalexample>
<programlisting>
$simple = &quot;&lt;para&gt;&lt;note&gt;simple note&lt;/note&gt;&lt;/para&gt;&quot;;
$p = xml_parser_create();
xml_parse_into_struct($p,$simple,&amp;$vals,&amp;$index);
xml_parser_free($p);
echo "Index array\n";
print_r($index);
echo "\nVals array\n";
print_r($vals);
</programlisting>
</informalexample>
When we run that code, the output will be:
<informalexample>
<programlisting>
Index array
Array
(
[PARA] =&gt; Array
(
[0] =&gt; 0
[1] =&gt; 2
)
[NOTE] =&gt; Array
(
[0] =&gt; 1
)
)
Vals array
Array
(
[0] =&gt; Array
(
[tag] =&gt; PARA
[type] =&gt; open
[level] =&gt; 1
)
[1] =&gt; Array
(
[tag] =&gt; NOTE
[type] =&gt; complete
[level] =&gt; 2
[value] =&gt; simple note
)
[2] =&gt; Array
(
[tag] =&gt; PARA
[type] =&gt; close
[level] =&gt; 1
)
)
</programlisting>
</informalexample>
</para>
<para>
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:
<example>
<title>moldb.xml - small database of molecular information</title>
<programlisting>
&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;moldb&gt;
&lt;molecule&gt;
&lt;name&gt;Alanine&lt;/name&gt;
&lt;symbol&gt;ala&lt;/symbol&gt;
&lt;code&gt;A&lt;/code&gt;
&lt;type&gt;hydrophobic&lt;/type&gt;
&lt;/molecule&gt;
&lt;molecule&gt;
&lt;name&gt;Lysine&lt;/name&gt;
&lt;symbol&gt;lys&lt;/symbol&gt;
&lt;code&gt;K&lt;/code&gt;
&lt;type&gt;charged&lt;/type&gt;
&lt;/molecule&gt;
&lt;/moldb&gt;
</programlisting>
</example>
And then code to parse the document and generate the appropriate
objects:
<example>
<title>
parsemoldb.php - parses moldb.xml into and array of
molecular objects
</title>
<programlisting role="php">
&lt;?
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=&gt;$v)
$this-&gt;$k = $aa[$k];
}
}
function readDatabase($filename) {
// read the xml database of aminoacids
$data = implode(&quot;&quot;,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,&amp;$values,&amp;$tags);
xml_parser_free($parser);
// loop through the structures
foreach ($tags as $key=&gt;$val) {
if ($key == &quot;molecule&quot;) {
$molranges = $val;
// each contiguous pair of array entries are the
// lower and upper range for each molecule definition
for ($i=0; $i &lt; 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 &lt; count($mvalues); $i++)
$mol[$mvalues[$i][&quot;tag&quot;]] = $mvalues[$i][&quot;value&quot;];
return new AminoAcid($mol);
}
$db = readDatabase(&quot;moldb.xml&quot;);
echo "** Database of AminoAcid objects:\n";
print_r($db);
?&gt;
</programlisting>
</example>
After executing <filename>parsemoldb.php</filename>, the variable
<varname>$db</varname> contains an array of
<classname>AminoAcid</classname> objects, and the output of the
script confirms that:
<informalexample>
<programlisting>
** Database of AminoAcid objects:
Array
(
[0] =&gt; aminoacid Object
(
[name] =&gt; Alanine
[symbol] =&gt; ala
[code] =&gt; A
[type] =&gt; hydrophobic
)
[1] =&gt; aminoacid Object
(
[name] =&gt; Lysine
[symbol] =&gt; lys
[code] =&gt; K
[type] =&gt; charged
)
)
</programlisting>
</informalexample>
</para>
</refsect1>
</refentry>
<refentry id="function.xml-parser-free">
<refnamediv>
<refname>xml_parser_free</refname>