<!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 > 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 > 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ã: </B > This extension requires PHP 5.</P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="simplexml.examples" >Exemple</A ></H1 ><P > 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 > <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"><?php<br />$xmlstr </font><font color="#007700">= <<<XML<br /></font><font color="#0000BB"><?xml version='1.0' standalone='yes'?><br /><movies><br /> <movie><br /> <title>PHP: Behind the Parser</title><br /> <characters><br /> <character><br /> <name>Ms. Coder</name><br /> <actor>Onlivia Actora</actor><br /> </character><br /> <character><br /> <name>Mr. Coder</name><br /> <actor>El Act&#211;r</actor><br /> </character><br /> </characters><br /> <plot><br /> So, this language. It's like, a programming language. Or is it a<br /> scripting language? All is revealed in this thrilling horror spoof<br /> of a documentary.<br /> </plot><br /> <rating type="thumbs">7</rating><br /> <rating type="stars">5</rating><br /> </movie><br /></movies><br /></font><font color="#007700">XML;<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > 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" ><plot></VAR ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?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">-></font><font color="#0000BB">movie</font><font color="#007700">[</font><font color="#0000BB">0</font><font color="#007700">]-></font><font color="#0000BB">plot</font><font color="#007700">; </font><font color="#FF8000">// "So this language. It's like..."<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > <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 > 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"><?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 <movie> node, we echo a separate <plot>. */<br /></font><font color="#007700">foreach (</font><font color="#0000BB">$xml</font><font color="#007700">-></font><font color="#0000BB">movie </font><font color="#007700">as </font><font color="#0000BB">$movie</font><font color="#007700">) {<br /> echo </font><font color="#0000BB">$movie</font><font color="#007700">-></font><font color="#0000BB">plot</font><font color="#007700">, </font><font color="#DD0000">'<br />'</font><font color="#007700">;<br />}<br /><br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > <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 > 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"><?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 <rating> 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">-></font><font color="#0000BB">movie</font><font color="#007700">[</font><font color="#0000BB">0</font><font color="#007700">]-></font><font color="#0000BB">rating </font><font color="#007700">as </font><font color="#0000BB">$rating</font><font color="#007700">) {<br /> 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 /> </font><font color="#007700">case </font><font color="#DD0000">'thumbs'</font><font color="#007700">:<br /> echo </font><font color="#0000BB">$rating</font><font color="#007700">, </font><font color="#DD0000">' thumbs up'</font><font color="#007700">;<br /> break;<br /> case </font><font color="#DD0000">'stars'</font><font color="#007700">:<br /> echo </font><font color="#0000BB">$rating</font><font color="#007700">, </font><font color="#DD0000">' stars'</font><font color="#007700">;<br /> break;<br /> }<br />}<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > <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 > 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"><?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 />if ((string) </font><font color="#0000BB">$xml</font><font color="#007700">-></font><font color="#0000BB">movie</font><font color="#007700">-></font><font color="#0000BB">title </font><font color="#007700">== </font><font color="#DD0000">'PHP: Behind the Parser'</font><font color="#007700">) {<br /> 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">-></font><font color="#0000BB">movie</font><font color="#007700">-></font><font color="#0000BB">title</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > <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 > SimpleXML includes builtin <ACRONYM CLASS="acronym" >Xpath</ACRONYM > support. To find all <VAR CLASS="literal" ><character></VAR > elements: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?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">-></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 /> echo </font><font color="#0000BB">$character</font><font color="#007700">-></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">-></font><font color="#0000BB">actor</font><font color="#007700">, </font><font color="#DD0000">'<br />'</font><font color="#007700">;<br />}<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P > '<VAR CLASS="literal" >//</VAR >' serves as a wildcard. To specify absolute paths, omit one of the slashes. </P ></DIV ></TD ></TR ></TABLE > </P ><P > <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 > 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"><?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">-></font><font color="#0000BB">movie</font><font color="#007700">[</font><font color="#0000BB">0</font><font color="#007700">]-></font><font color="#0000BB">actor</font><font color="#007700">[</font><font color="#0000BB">0</font><font color="#007700">]-></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">-></font><font color="#0000BB">asXML</font><font color="#007700">();<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P > 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 > <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 > 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"><?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">-></font><font color="#0000BB">loadXML</font><font color="#007700">(</font><font color="#DD0000">'<books><book><title>blah</title></book></books>'</font><font color="#007700">);<br />if (!</font><font color="#0000BB">$dom</font><font color="#007700">) {<br /> echo </font><font color="#DD0000">'Error while parsing the document'</font><font color="#007700">;<br /> 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">-></font><font color="#0000BB">book</font><font color="#007700">[</font><font color="#0000BB">0</font><font color="#007700">]-></font><font color="#0000BB">title</font><font color="#007700">;<br /></font><font color="#0000BB">?></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->asXML</A > -- Return a well-formed XML string based on SimpleXML element. </DT ><DT ><A HREF="function.simplexml-element-attributes.html" >simplexml_element->attributes</A > -- Identifies an element's attributes. </DT ><DT ><A HREF="function.simplexml-element-children.html" >simplexml_element->children</A > -- Finds children of given node. </DT ><DT ><A HREF="function.simplexml-element-xpath.html" >simplexml_element->xpath</A > -- Runs Xpath query on XML data. </DT ><DT ><A HREF="function.simplexml-import-dom.html" >simplexml_import_dom</A > -- 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 > -- Interprets an XML file into an object. </DT ><DT ><A HREF="function.simplexml-load-string.html" >simplexml_load_string</A > -- 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ã</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->asXML</TD ></TR ></TABLE ></DIV ></BODY ></HTML >