<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >SimpleXML functions</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="Referência das Funções" HREF="funcref.html"><LINK REL="PREVIOUS" TITLE="shmop_write" HREF="function.shmop-write.html"><LINK REL="NEXT" TITLE="SimpleXMLElement->addAttribute()" HREF="function.simplexml-element-addattribute.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8"></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 do PHP</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.shmop-write.html" ACCESSKEY="P" >Anterior</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.simplexml-element-addattribute.html" ACCESSKEY="N" >Próxima</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="reference" ><A NAME="ref.simplexml" ></A ><DIV CLASS="TITLEPAGE" ><H1 CLASS="title" >CLI. SimpleXML functions</H1 ><DIV CLASS="PARTINTRO" ><A NAME="AEN204453" ></A ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="simplexml.intro" >Introdução</A ></H1 ><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.requirements" >Dependências</A ></H1 ><P > The SimpleXML extension requires PHP 5. </P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="simplexml.installation" >Instalação</A ></H1 ><P > The SimpleXML extension is enabled by default. To disable it, use the <CODE CLASS="option" >--disable-simplexml</CODE > configure option. </P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="simplexml.examples" >Exemplos</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="AEN204469" ></A ><P ><B >Exemplo 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="AEN204473" ></A ><P ><B >Exemplo 2. Getting <TT CLASS="literal" ><plot></TT ></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">= new </font><font color="#0000BB">SimpleXMLElement</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="AEN204478" ></A ><P ><B >Exemplo 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">= new </font><font color="#0000BB">SimpleXMLElement</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="AEN204483" ></A ><P ><B >Exemplo 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">= new </font><font color="#0000BB">SimpleXMLElement</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="AEN204489" ></A ><P ><B >Exemplo 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 <TT CLASS="literal" >(string)</TT >. 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">= new </font><font color="#0000BB">SimpleXMLElement</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="AEN204495" ></A ><P ><B >Exemplo 6. Using XPath</B ></P ><P > SimpleXML includes built-in <ACRONYM CLASS="acronym" >XPath</ACRONYM > support. To find all <TT CLASS="literal" ><character></TT > 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">= new </font><font color="#0000BB">SimpleXMLElement</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 > '<TT CLASS="literal" >//</TT >' 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="AEN204504" ></A ><P ><B >Exemplo 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">= new </font><font color="#0000BB">SimpleXMLElement</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">characters</font><font color="#007700">-></font><font color="#0000BB">character</font><font color="#007700">[</font><font color="#0000BB">0</font><font color="#007700">]-></font><font color="#0000BB">name </font><font color="#007700">= </font><font color="#DD0000">'Miss Coder'</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 change Ms. Coder to Miss Coder. </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="AEN204510" ></A ><P ><B >Exemplo 8. Adding elements and attributes</B ></P ><P > Since PHP 5.1.3, SimpleXML has had the ability to easily add children and attributes. </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">= new </font><font color="#0000BB">SimpleXMLElement</font><font color="#007700">(</font><font color="#0000BB">$xmlstr</font><font color="#007700">);<br /><br /></font><font color="#0000BB">$character </font><font color="#007700">= </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">characters</font><font color="#007700">-></font><font color="#0000BB">addChild</font><font color="#007700">(</font><font color="#DD0000">'character'</font><font color="#007700">);<br /></font><font color="#0000BB">$character</font><font color="#007700">-></font><font color="#0000BB">addChild</font><font color="#007700">(</font><font color="#DD0000">'name'</font><font color="#007700">, </font><font color="#DD0000">'Mr. Parser'</font><font color="#007700">);<br /></font><font color="#0000BB">$character</font><font color="#007700">-></font><font color="#0000BB">addChild</font><font color="#007700">(</font><font color="#DD0000">'actor'</font><font color="#007700">, </font><font color="#DD0000">'John Doe'</font><font color="#007700">);<br /><br /></font><font color="#0000BB">$rating </font><font color="#007700">= </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">addChild</font><font color="#007700">(</font><font color="#DD0000">'rating'</font><font color="#007700">, </font><font color="#DD0000">'PG'</font><font color="#007700">);<br /></font><font color="#0000BB">$rating</font><font color="#007700">-></font><font color="#0000BB">addAttribute</font><font color="#007700">(</font><font color="#DD0000">'type'</font><font color="#007700">, </font><font color="#DD0000">'mpaa'</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 an XML document based on the original but having a new character and rating. </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="AEN204516" ></A ><P ><B >Exemplo 9. 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 >Índice</B ></DT ><DT ><A HREF="function.simplexml-element-addattribute.html" >SimpleXMLElement->addAttribute()</A > -- Adds an attribute to the SimpleXML element </DT ><DT ><A HREF="function.simplexml-element-addchild.html" >SimpleXMLElement->addChild()</A > -- Adds a child element to the XML node </DT ><DT ><A HREF="function.simplexml-element-asxml.html" >SimpleXMLElement->asXML()</A > -- Return a well-formed XML string based on SimpleXML element </DT ><DT ><A HREF="function.simplexml-element-attributes.html" >SimpleXMLElement->attributes()</A > -- Identifies an element's attributes</DT ><DT ><A HREF="function.simplexml-element-children.html" >SimpleXMLElement->children()</A > -- Finds children of given node</DT ><DT ><A HREF="function.simplexml-element-construct.html" >SimpleXMLElement->__construct()</A > -- Creates a new SimpleXMLElement object </DT ><DT ><A HREF="function.simplexml-element-getdocnamespaces.html" >SimpleXMLElement->getDocNamespaces()</A > -- Returns namespaces declared in document </DT ><DT ><A HREF="function.simplexml-element-getname.html" >SimpleXMLElement->getName()</A > -- Gets the name of the XML element</DT ><DT ><A HREF="function.simplexml-element-getnamespaces.html" >SimpleXMLElement->getNamespaces()</A > -- Returns namespaces used in document </DT ><DT ><A HREF="function.simplexml-element-registerxpathnamespace.html" >SimpleXMLElement->registerXPathNamespace()</A > -- Creates a prefix/ns context for the next XPath query </DT ><DT ><A HREF="function.simplexml-element-xpath.html" >SimpleXMLElement->xpath()</A > -- Runs XPath query on XML data</DT ><DT ><A HREF="function.simplexml-import-dom.html" >simplexml_import_dom</A > -- Get a <TT CLASS="literal" >SimpleXMLElement</TT > 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" >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.simplexml-element-addattribute.html" ACCESSKEY="N" >Próxima</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" >Acima</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >SimpleXMLElement->addAttribute()</TD ></TR ></TABLE ></DIV ></BODY ></HTML >