Sophie

Sophie

distrib > Mandriva > 9.1 > ppc > media > main > by-pkgid > 0afeee9cca140e167a996902b9a677c5 > files > 2841

php-manual-en-4.3.0-2mdk.noarch.rpm

<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&#62;= 3.0.8, PHP 4 )</P
>xml_parse_into_struct&nbsp;--&nbsp;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 &#38;values [, array &#38;index])<BR
></BR
><P
>&#13;     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
>&#13;     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 = "&#60;para&#62;&#60;note&#62;simple note&#60;/note&#62;&#60;/para&#62;";
$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] =&#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;     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"
>&#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
>
     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"
>&#60;?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=&#62;$v)
            $this-&#62;$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=&#62;$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 &#60; 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 &#60; 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);

?&#62;</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] =&#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"
>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
>