Sophie

Sophie

distrib > Mandriva > 2008.1 > i586 > by-pkgid > 703d980c580707c382b4e43e25965bc5 > files > 10387

php-manual-pt_BR-5.2.4-1mdv2008.1.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>xml_parse_into_struct</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REL="HOME"
TITLE="Manual do PHP"
HREF="index.html"><LINK
REL="UP"
TITLE="XML, Analisadores"
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=UTF-8"></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"
>Manual do PHP</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="function.xml-get-error-code.html"
ACCESSKEY="P"
>Anterior</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"
>Próxima</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="AEN234208"
></A
><P
>    (PHP 3 &#62;= 3.0.8, PHP 4, PHP 5)</P
>xml_parse_into_struct&nbsp;--&nbsp;Analisa dados XML dentro de uma estrutura de 
	array </DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN234211"
></A
><H2
>Descrição</H2
>int <B
CLASS="methodname"
>xml_parse_into_struct</B
> ( resource parser, string data, array &#38;values [, array &#38;index] )<BR
></BR
><P
>&#13;     Esta função analisa uma arquivo XML dentro de 2  estruturas de array
	 paralelas, um (<CODE
CLASS="parameter"
>index</CODE
>) contendo indicadores para a
	 localização dos valores apropriados nos <CODE
CLASS="parameter"
>values</CODE
> do
	 array. Estes dois últimos parâmetros deve ser passados por referência.
    </P
><P
>&#13;     Abaixo tem um exemplo que ilustra a estrutura interna dos arrays sendo gerados pela função. Nós usamos uma simples <TT
CLASS="literal"
>note</TT
> tag imbutida dentro da
     <TT
CLASS="literal"
>para</TT
> tag, e quando nós analisamos isto exibe as estruturas geredas:
     <DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN234234"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br />$simple </font><font color="#007700">= </font><font color="#DD0000">"&lt;para&gt;&lt;note&gt;simple note&lt;/note&gt;&lt;/para&gt;"</font><font color="#007700">;<br /></font><font color="#0000BB">$p </font><font color="#007700">= </font><font color="#0000BB">xml_parser_create</font><font color="#007700">();<br /></font><font color="#0000BB">xml_parse_into_struct</font><font color="#007700">(</font><font color="#0000BB">$p</font><font color="#007700">, </font><font color="#0000BB">$simple</font><font color="#007700">, </font><font color="#0000BB">$vals</font><font color="#007700">, </font><font color="#0000BB">$index</font><font color="#007700">);<br /></font><font color="#0000BB">xml_parser_free</font><font color="#007700">(</font><font color="#0000BB">$p</font><font color="#007700">);<br />echo </font><font color="#DD0000">"Index array\n"</font><font color="#007700">;<br /></font><font color="#0000BB">print_r</font><font color="#007700">(</font><font color="#0000BB">$index</font><font color="#007700">);<br />echo </font><font color="#DD0000">"\nVals array\n"</font><font color="#007700">;<br /></font><font color="#0000BB">print_r</font><font color="#007700">(</font><font color="#0000BB">$vals</font><font color="#007700">);<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
>&#13;       Quando nós executarmos o código, a saída será:
      </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="screen"
>Index array
Array
(
    [PARA] =&#62; Array
        (
            [0] =&#62; 0
            [1] =&#62; 2
        )

    [NOTE] =&#62; Array
        (
            [0] =&#62; 1
        )

)

Vals array
Array
(
    [0] =&#62; Array
        (
            [tag] =&#62; PARA
            [type] =&#62; open
            [level] =&#62; 1
        )

    [1] =&#62; Array
        (
            [tag] =&#62; NOTE
            [type] =&#62; complete
            [level] =&#62; 2
            [value] =&#62; simple note
        )

    [2] =&#62; Array
        (
            [tag] =&#62; PARA
            [type] =&#62; close
            [level] =&#62; 1
        )

)</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
    </P
><P
>&#13;     Análise dirigida por eventos (Event-driven parsing) (baseada
	 nabiblioteca do expat) pode complicado quando você tem um documento
	 XML que é complexo. Esta função não produz um objeto no estilo DOM,
     mas gera estruturas cômodas de serem  organizadas em uma forma de
	 árvore. Assim, nós podemos criar objetos representando os dados nos
	 arquivos facilmente. Vamos considerar o seguinte arquivo representando um pequeno banco de dados de informações de aminoácidos:
     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN234239"
></A
><P
><B
>Exemplo 1. moldb.xml - pequeno banco de dados de informações 
       moleculares</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="xml"
>&#60;?xml version="1.0"?&#62;
&#60;moldb&#62;

    &#60;molecule&#62;
        &#60;name&#62;Alanine&#60;/name&#62;
        &#60;symbol&#62;ala&#60;/symbol&#62;
        &#60;code&#62;A&#60;/code&#62;
        &#60;type&#62;hydrophobic&#60;/type&#62;
    &#60;/molecule&#62;

    &#60;molecule&#62;
        &#60;name&#62;Lysine&#60;/name&#62;
        &#60;symbol&#62;lys&#60;/symbol&#62;
        &#60;code&#62;K&#60;/code&#62;
        &#60;type&#62;charged&#60;/type&#62;
    &#60;/molecule&#62;

&#60;/moldb&#62;</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
     E alguns códigos para analisar o documento e gerar os obejetos 
     apropriados:
     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN234242"
></A
><P
><B
>Exemplo 2. 
       parsemoldb.php - analisa moldb.xml e cria o array dos objetos moleculares
      </B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /><br /></font><font color="#007700">class </font><font color="#0000BB">AminoAcid </font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;var </font><font color="#0000BB">$name</font><font color="#007700">;&nbsp;&nbsp;</font><font color="#FF8000">// aa name<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">var </font><font color="#0000BB">$symbol</font><font color="#007700">;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// three letter symbol<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">var </font><font color="#0000BB">$code</font><font color="#007700">;&nbsp;&nbsp;</font><font color="#FF8000">// one letter code<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">var </font><font color="#0000BB">$type</font><font color="#007700">;&nbsp;&nbsp;</font><font color="#FF8000">// hydrophobic, charged or neutral<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">function </font><font color="#0000BB">AminoAcid </font><font color="#007700">(</font><font color="#0000BB">$aa</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foreach (</font><font color="#0000BB">$aa </font><font color="#007700">as </font><font color="#0000BB">$k</font><font color="#007700">=&gt;</font><font color="#0000BB">$v</font><font color="#007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">$k </font><font color="#007700">= </font><font color="#0000BB">$aa</font><font color="#007700">[</font><font color="#0000BB">$k</font><font color="#007700">];<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br />function </font><font color="#0000BB">readDatabase</font><font color="#007700">(</font><font color="#0000BB">$filename</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// lê o banco de dados XML de aminoácidos<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$data </font><font color="#007700">= </font><font color="#0000BB">implode</font><font color="#007700">(</font><font color="#DD0000">""</font><font color="#007700">, </font><font color="#0000BB">file</font><font color="#007700">(</font><font color="#0000BB">$filename</font><font color="#007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$parser </font><font color="#007700">= </font><font color="#0000BB">xml_parser_create</font><font color="#007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">xml_parser_set_option</font><font color="#007700">(</font><font color="#0000BB">$parser</font><font color="#007700">, </font><font color="#0000BB">XML_OPTION_CASE_FOLDING</font><font color="#007700">, </font><font color="#0000BB">0</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">xml_parser_set_option</font><font color="#007700">(</font><font color="#0000BB">$parser</font><font color="#007700">, </font><font color="#0000BB">XML_OPTION_SKIP_WHITE</font><font color="#007700">, </font><font color="#0000BB">1</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">xml_parse_into_struct</font><font color="#007700">(</font><font color="#0000BB">$parser</font><font color="#007700">, </font><font color="#0000BB">$data</font><font color="#007700">, </font><font color="#0000BB">$values</font><font color="#007700">, </font><font color="#0000BB">$tags</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">xml_parser_free</font><font color="#007700">(</font><font color="#0000BB">$parser</font><font color="#007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// loop through the structures<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">foreach (</font><font color="#0000BB">$tags </font><font color="#007700">as </font><font color="#0000BB">$key</font><font color="#007700">=&gt;</font><font color="#0000BB">$val</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (</font><font color="#0000BB">$key </font><font color="#007700">== </font><font color="#DD0000">"molecule"</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$molranges </font><font color="#007700">= </font><font color="#0000BB">$val</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// each contiguous pair of array entries are the <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// lower and upper range for each molecule definition<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">for (</font><font color="#0000BB">$i</font><font color="#007700">=</font><font color="#0000BB">0</font><font color="#007700">; </font><font color="#0000BB">$i </font><font color="#007700">&lt; </font><font color="#0000BB">count</font><font color="#007700">(</font><font color="#0000BB">$molranges</font><font color="#007700">); </font><font color="#0000BB">$i</font><font color="#007700">+=</font><font color="#0000BB">2</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$offset </font><font color="#007700">= </font><font color="#0000BB">$molranges</font><font color="#007700">[</font><font color="#0000BB">$i</font><font color="#007700">] + </font><font color="#0000BB">1</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$len </font><font color="#007700">= </font><font color="#0000BB">$molranges</font><font color="#007700">[</font><font color="#0000BB">$i </font><font color="#007700">+ </font><font color="#0000BB">1</font><font color="#007700">] - </font><font color="#0000BB">$offset</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$tdb</font><font color="#007700">[] = </font><font color="#0000BB">parseMol</font><font color="#007700">(</font><font color="#0000BB">array_slice</font><font color="#007700">(</font><font color="#0000BB">$values</font><font color="#007700">, </font><font color="#0000BB">$offset</font><font color="#007700">, </font><font color="#0000BB">$len</font><font color="#007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;continue;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;return </font><font color="#0000BB">$tdb</font><font color="#007700">;<br />}<br /><br />function </font><font color="#0000BB">parseMol</font><font color="#007700">(</font><font color="#0000BB">$mvalues</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;for (</font><font color="#0000BB">$i</font><font color="#007700">=</font><font color="#0000BB">0</font><font color="#007700">; </font><font color="#0000BB">$i </font><font color="#007700">&lt; </font><font color="#0000BB">count</font><font color="#007700">(</font><font color="#0000BB">$mvalues</font><font color="#007700">); </font><font color="#0000BB">$i</font><font color="#007700">++)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$mol</font><font color="#007700">[</font><font color="#0000BB">$mvalues</font><font color="#007700">[</font><font color="#0000BB">$i</font><font color="#007700">][</font><font color="#DD0000">"tag"</font><font color="#007700">]] = </font><font color="#0000BB">$mvalues</font><font color="#007700">[</font><font color="#0000BB">$i</font><font color="#007700">][</font><font color="#DD0000">"value"</font><font color="#007700">];<br />&nbsp;&nbsp;&nbsp;&nbsp;return new </font><font color="#0000BB">AminoAcid</font><font color="#007700">(</font><font color="#0000BB">$mol</font><font color="#007700">);<br />}<br /><br /></font><font color="#0000BB">$db </font><font color="#007700">= </font><font color="#0000BB">readDatabase</font><font color="#007700">(</font><font color="#DD0000">"moldb.xml"</font><font color="#007700">);<br />echo </font><font color="#DD0000">"** Database of AminoAcid objects:\n"</font><font color="#007700">;<br /></font><font color="#0000BB">print_r</font><font color="#007700">(</font><font color="#0000BB">$db</font><font color="#007700">);<br /><br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
     After executing <TT
CLASS="filename"
>parsemoldb.php</TT
>, a variável
     <CODE
CLASS="varname"
>$db</CODE
> contém um array dos objetos de
     <B
CLASS="classname"
>AminoAcid</B
>, e a saída do script confirma isso:
	 <DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN234248"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="screen"
>** Database of AminoAcid objects:
Array
(
    [0] =&#62; aminoacid Object
        (
            [name] =&#62; Alanine
            [symbol] =&#62; ala
            [code] =&#62; A
            [type] =&#62; hydrophobic
        )

    [1] =&#62; aminoacid Object
        (
            [name] =&#62; Lysine
            [symbol] =&#62; lys
            [code] =&#62; K
            [type] =&#62; 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"
>Anterior</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Principal</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="function.xml-parse.html"
ACCESSKEY="N"
>Próxima</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"
>Acima</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>xml_parse</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>