Sophie

Sophie

distrib > PLD > ac > amd64 > media > dist > by-pkgid > dd8ef74e7a184506d40e4328053fb785 > files > 3902

php-manual-ro-20051028-1.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>SimpleXML functions</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="Manual PHP"
HREF="index.html"><LINK
REL="UP"
TITLE="Documentaþia funcþiilor"
HREF="funcref.html"><LINK
REL="PREVIOUS"
TITLE="shmop_write"
HREF="function.shmop-write.html"><LINK
REL="NEXT"
TITLE="simplexml_element->asXML"
HREF="function.simplexml-element-asxml.html"><META
HTTP-EQUIV="Content-type"
CONTENT="text/html; charset=ISO-8859-2"></HEAD
><BODY
CLASS="reference"
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 PHP</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="function.shmop-write.html"
ACCESSKEY="P"
>Înapoi</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="function.simplexml-element-asxml.html"
ACCESSKEY="N"
>Înainte</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="reference"
><A
NAME="ref.simplexml"
></A
><DIV
CLASS="TITLEPAGE"
><H1
CLASS="title"
>CI. SimpleXML functions</H1
><DIV
CLASS="PARTINTRO"
><A
NAME="AEN103796"
></A
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="simplexml.intro"
>Introducere</A
></H1
><DIV
CLASS="warning"
><P
></P
><TABLE
CLASS="warning"
BORDER="1"
WIDTH="100%"
><TR
><TD
ALIGN="CENTER"
><B
>Avertisment</B
></TD
></TR
><TR
><TD
ALIGN="LEFT"
><P
>Aceastã extensie este
<SPAN
CLASS="emphasis"
><I
CLASS="emphasis"
>EXPERIMENTALÃ</I
></SPAN
>. Comportamentul acestei extensii,
incluzând numele funcþiilor ºi orice altceva documentat despre
aceastã extensie ar putea fi schimbate farã un anunþ prealabil,
intr-o viitoare versiune PHP. V-am avertizat, deci folosiþi aceastã
extensie pe riscul dv.</P
></TD
></TR
></TABLE
></DIV
><P
>&#13;     The SimpleXML extension provides a very simple and easily usable
     toolset to convert XML to an object that can be processed with
     normal property selectors and array iterators.
    </P
></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="simplexml.installation"
>Instalare</A
></H1
><P
>&#13;     This extension is only available if PHP was configured with
     <VAR
CLASS="option"
>--enable-simplexml</VAR
>. The
     PHP configuration script does this by default.
    </P
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>Not&#227;: </B
>
This extension requires PHP 5.</P
></BLOCKQUOTE
></DIV
></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="simplexml.examples"
>Exemple</A
></H1
><P
>&#13;     Many examples in this reference require an XML string. Instead of
     repeating this string in every example, we put it into a file which
     we include in each example. This included file is shown in the 
     following example section. Alternatively, you could create an XML
     document and read it with <A
HREF="function.simplexml-load-file.html"
><B
CLASS="function"
>simplexml_load_file()</B
></A
>.
    </P
><P
>&#13;     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN103814"
></A
><P
><B
>Exemplu 1. Include file example.php with XML string</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br />$xmlstr </font><font color="#007700">= &lt;&lt;&lt;XML<br /></font><font color="#0000BB">&lt;?xml version='1.0' standalone='yes'?&gt;<br />&lt;movies&gt;<br /> &lt;movie&gt;<br />&nbsp;&nbsp;&lt;title&gt;PHP: Behind the Parser&lt;/title&gt;<br />&nbsp;&nbsp;&lt;characters&gt;<br />&nbsp;&nbsp;&nbsp;&lt;character&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;name&gt;Ms. Coder&lt;/name&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;actor&gt;Onlivia Actora&lt;/actor&gt;<br />&nbsp;&nbsp;&nbsp;&lt;/character&gt;<br />&nbsp;&nbsp;&nbsp;&lt;character&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;name&gt;Mr. Coder&lt;/name&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;actor&gt;El Act&amp;#211;r&lt;/actor&gt;<br />&nbsp;&nbsp;&nbsp;&lt;/character&gt;<br />&nbsp;&nbsp;&lt;/characters&gt;<br />&nbsp;&nbsp;&lt;plot&gt;<br />&nbsp;&nbsp;&nbsp;So, this language. It's like, a programming language. Or is it a<br />&nbsp;&nbsp;&nbsp;scripting language? All is revealed in this thrilling horror spoof<br />&nbsp;&nbsp;&nbsp;of a documentary.<br />&nbsp;&nbsp;&lt;/plot&gt;<br />&nbsp;&nbsp;&lt;rating type="thumbs"&gt;7&lt;/rating&gt;<br />&nbsp;&nbsp;&lt;rating type="stars"&gt;5&lt;/rating&gt;<br /> &lt;/movie&gt;<br />&lt;/movies&gt;<br /></font><font color="#007700">XML;<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
    </P
><P
>&#13;     The simplicity of SimpleXML appears most clearly when one extracts
     a string or number from a basic XML document.
     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN103818"
></A
><P
><B
>Exemplu 2. Getting <VAR
CLASS="literal"
>&#60;plot&#62;</VAR
></B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#007700">include </font><font color="#DD0000">'example.php'</font><font color="#007700">;<br /><br /></font><font color="#0000BB">$xml </font><font color="#007700">= </font><font color="#0000BB">simplexml_load_string</font><font color="#007700">(</font><font color="#0000BB">$xmlstr</font><font color="#007700">);<br /><br />echo </font><font color="#0000BB">$xml</font><font color="#007700">-&gt;</font><font color="#0000BB">movie</font><font color="#007700">[</font><font color="#0000BB">0</font><font color="#007700">]-&gt;</font><font color="#0000BB">plot</font><font color="#007700">; </font><font color="#FF8000">// "So this language. It's like..."<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
    </P
><P
>&#13;     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN103823"
></A
><P
><B
>Exemplu 3. Accessing non-unique elements in SimpleXML</B
></P
><P
>&#13;       When multiple instances of an element exist as children of
       a single parent element, normal iteration techniques apply.
      </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#007700">include </font><font color="#DD0000">'example.php'</font><font color="#007700">;<br /><br /></font><font color="#0000BB">$xml </font><font color="#007700">= </font><font color="#0000BB">simplexml_load_string</font><font color="#007700">(</font><font color="#0000BB">$xmlstr</font><font color="#007700">);<br /><br /></font><font color="#FF8000">/* For each &lt;movie&gt; node, we echo a separate &lt;plot&gt;. */<br /></font><font color="#007700">foreach (</font><font color="#0000BB">$xml</font><font color="#007700">-&gt;</font><font color="#0000BB">movie </font><font color="#007700">as </font><font color="#0000BB">$movie</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;echo </font><font color="#0000BB">$movie</font><font color="#007700">-&gt;</font><font color="#0000BB">plot</font><font color="#007700">, </font><font color="#DD0000">'&lt;br /&gt;'</font><font color="#007700">;<br />}<br /><br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
    </P
><P
>&#13;     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN103828"
></A
><P
><B
>Exemplu 4. Using attributes</B
></P
><P
>&#13;       So far, we have only covered the work of reading element names
       and their values. SimpleXML can also access element attributes.
       Access attributes of an element just as you would elements
       of an <A
HREF="language.types.array.html"
><B
CLASS="type"
>array</B
></A
>.
      </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#007700">include </font><font color="#DD0000">'example.php'</font><font color="#007700">;<br /><br /></font><font color="#0000BB">$xml </font><font color="#007700">= </font><font color="#0000BB">simplexml_load_string</font><font color="#007700">(</font><font color="#0000BB">$xmlstr</font><font color="#007700">);<br /><br /></font><font color="#FF8000">/* Access the &lt;rating&gt; nodes of the first movie.<br /> * Output the rating scale, too. */<br /></font><font color="#007700">foreach (</font><font color="#0000BB">$xml</font><font color="#007700">-&gt;</font><font color="#0000BB">movie</font><font color="#007700">[</font><font color="#0000BB">0</font><font color="#007700">]-&gt;</font><font color="#0000BB">rating </font><font color="#007700">as </font><font color="#0000BB">$rating</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;switch((string) </font><font color="#0000BB">$rating</font><font color="#007700">[</font><font color="#DD0000">'type'</font><font color="#007700">]) { </font><font color="#FF8000">// Get attributes as element indices<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">case </font><font color="#DD0000">'thumbs'</font><font color="#007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#0000BB">$rating</font><font color="#007700">, </font><font color="#DD0000">' thumbs up'</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;case </font><font color="#DD0000">'stars'</font><font color="#007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#0000BB">$rating</font><font color="#007700">, </font><font color="#DD0000">' stars'</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
    </P
><P
>&#13;     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN103834"
></A
><P
><B
>Exemplu 5. Comparing Elements and Attributes with Text</B
></P
><P
>&#13;       To compare an element or attribute with a string or pass it into a 
       function that requires a string, you must cast it to a string using 
       <VAR
CLASS="literal"
>(string)</VAR
>. Otherwise, PHP treats the element as an object.
      </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php &nbsp;&nbsp;&nbsp;&nbsp;<br /></font><font color="#007700">include </font><font color="#DD0000">'example.php'</font><font color="#007700">;<br /><br /></font><font color="#0000BB">$xml </font><font color="#007700">= </font><font color="#0000BB">simplexml_load_string</font><font color="#007700">(</font><font color="#0000BB">$xmlstr</font><font color="#007700">);<br /><br />if ((string) </font><font color="#0000BB">$xml</font><font color="#007700">-&gt;</font><font color="#0000BB">movie</font><font color="#007700">-&gt;</font><font color="#0000BB">title </font><font color="#007700">== </font><font color="#DD0000">'PHP: Behind the Parser'</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;print </font><font color="#DD0000">'My favorite movie.'</font><font color="#007700">;<br />}<br /><br /></font><font color="#0000BB">htmlentities</font><font color="#007700">((string) </font><font color="#0000BB">$xml</font><font color="#007700">-&gt;</font><font color="#0000BB">movie</font><font color="#007700">-&gt;</font><font color="#0000BB">title</font><font color="#007700">);<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
    </P
><P
>&#13;     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN103840"
></A
><P
><B
>Exemplu 6. Using Xpath</B
></P
><P
>&#13;       SimpleXML includes builtin <ACRONYM
CLASS="acronym"
>Xpath</ACRONYM
> support.
       To find all <VAR
CLASS="literal"
>&#60;character&#62;</VAR
> elements:
      </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#007700">include </font><font color="#DD0000">'example.php'</font><font color="#007700">;<br /></font><font color="#0000BB">$xml </font><font color="#007700">= </font><font color="#0000BB">simplexml_load_string</font><font color="#007700">(</font><font color="#0000BB">$xmlstr</font><font color="#007700">);<br /><br />foreach (</font><font color="#0000BB">$xml</font><font color="#007700">-&gt;</font><font color="#0000BB">xpath</font><font color="#007700">(</font><font color="#DD0000">'//character'</font><font color="#007700">) as </font><font color="#0000BB">$character</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#0000BB">$character</font><font color="#007700">-&gt;</font><font color="#0000BB">name</font><font color="#007700">, </font><font color="#DD0000">'played by '</font><font color="#007700">, </font><font color="#0000BB">$character</font><font color="#007700">-&gt;</font><font color="#0000BB">actor</font><font color="#007700">, </font><font color="#DD0000">'&lt;br /&gt;'</font><font color="#007700">;<br />}<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
>&#13;       '<VAR
CLASS="literal"
>//</VAR
>' serves as a wildcard. To specify absolute
       paths, omit one of the slashes.
      </P
></DIV
></TD
></TR
></TABLE
>
    </P
><P
>&#13;     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN103849"
></A
><P
><B
>Exemplu 7. Setting values</B
></P
><P
>&#13;       Data in SimpleXML doesn't have to be constant. The object allows
       for manipulation of all of its elements.
      </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#007700">include </font><font color="#DD0000">'example.php'</font><font color="#007700">;<br /></font><font color="#0000BB">$xml </font><font color="#007700">= </font><font color="#0000BB">simplexml_load_string</font><font color="#007700">(</font><font color="#0000BB">$xmlstr</font><font color="#007700">);<br /><br /></font><font color="#0000BB">$xml</font><font color="#007700">-&gt;</font><font color="#0000BB">movie</font><font color="#007700">[</font><font color="#0000BB">0</font><font color="#007700">]-&gt;</font><font color="#0000BB">actor</font><font color="#007700">[</font><font color="#0000BB">0</font><font color="#007700">]-&gt;</font><font color="#0000BB">age </font><font color="#007700">= </font><font color="#DD0000">'21'</font><font color="#007700">;<br /><br />echo </font><font color="#0000BB">$xml</font><font color="#007700">-&gt;</font><font color="#0000BB">asXML</font><font color="#007700">();<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
>&#13;       The above code will output a new XML document, just like the original,
       except that the new XML will define Ms. Coder's age as 21.
      </P
></DIV
></TD
></TR
></TABLE
>
    </P
><P
>&#13;     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN103855"
></A
><P
><B
>Exemplu 8. DOM Interoperability</B
></P
><P
>&#13;       PHP has a mechanism to convert XML nodes between SimpleXML
       and DOM formats. This example shows how one might change
       a DOM element to SimpleXML.
      </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br />$dom </font><font color="#007700">= new </font><font color="#0000BB">domDocument</font><font color="#007700">;<br /></font><font color="#0000BB">$dom</font><font color="#007700">-&gt;</font><font color="#0000BB">loadXML</font><font color="#007700">(</font><font color="#DD0000">'&lt;books&gt;&lt;book&gt;&lt;title&gt;blah&lt;/title&gt;&lt;/book&gt;&lt;/books&gt;'</font><font color="#007700">);<br />if (!</font><font color="#0000BB">$dom</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">'Error while parsing the document'</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit;<br />}<br /><br /></font><font color="#0000BB">$s </font><font color="#007700">= </font><font color="#0000BB">simplexml_import_dom</font><font color="#007700">(</font><font color="#0000BB">$dom</font><font color="#007700">);<br /><br />echo </font><font color="#0000BB">$s</font><font color="#007700">-&gt;</font><font color="#0000BB">book</font><font color="#007700">[</font><font color="#0000BB">0</font><font color="#007700">]-&gt;</font><font color="#0000BB">title</font><font color="#007700">;<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
    </P
></DIV
></DIV
><DIV
CLASS="TOC"
><DL
><DT
><B
>Cuprins</B
></DT
><DT
><A
HREF="function.simplexml-element-asxml.html"
>simplexml_element-&#62;asXML</A
>&nbsp;--&nbsp;
     Return a well-formed XML string based on SimpleXML element.
    </DT
><DT
><A
HREF="function.simplexml-element-attributes.html"
>simplexml_element-&#62;attributes</A
>&nbsp;--&nbsp;
     Identifies an element's attributes.
    </DT
><DT
><A
HREF="function.simplexml-element-children.html"
>simplexml_element-&#62;children</A
>&nbsp;--&nbsp;
     Finds children of given node.
    </DT
><DT
><A
HREF="function.simplexml-element-xpath.html"
>simplexml_element-&#62;xpath</A
>&nbsp;--&nbsp;
     Runs Xpath query on XML data.
    </DT
><DT
><A
HREF="function.simplexml-import-dom.html"
>simplexml_import_dom</A
>&nbsp;--&nbsp;
     Get a <VAR
CLASS="literal"
>simplexml_element</VAR
> object from a
     DOM node.
    </DT
><DT
><A
HREF="function.simplexml-load-file.html"
>simplexml_load_file</A
>&nbsp;--&nbsp;
     Interprets an XML file into an object.
    </DT
><DT
><A
HREF="function.simplexml-load-string.html"
>simplexml_load_string</A
>&nbsp;--&nbsp;
     Interprets a string of XML into an object.
    </DT
></DL
></DIV
></DIV
></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.shmop-write.html"
ACCESSKEY="P"
>Înapoi</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Acas&#227;</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="function.simplexml-element-asxml.html"
ACCESSKEY="N"
>Înainte</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>shmop_write</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="funcref.html"
ACCESSKEY="U"
>Sus</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>simplexml_element-&#62;asXML</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>