simplexml.examples.html

来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 198 行 · 第 1/2 页

HTML
198
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head>  <title>Examples</title>  <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="simplexml.constants.html">Predefined Constants</a></div> <div class="next" style="text-align: right; float: right;"><a href="ref.simplexml.html">SimpleXML Functions</a></div> <div class="up"><a href="book.simplexml.html">SimpleXML</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div> <h1>Examples</h1> <div id="simplexml.examples-basic" class="section">  <p class="para">   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" class="function">simplexml_load_file()</a>.  </p>  <p class="para">   <div class="example">    <p><b>Example #1 Include file example.php with XML string</b></p>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$xmlstr&nbsp;</span><span style="color: #007700">=&nbsp;&lt;&lt;&lt;XML<br /></span><span style="color: #0000BB">&lt;?xml&nbsp;version='1.0'&nbsp;standalone='yes'?&gt;<br />&lt;movies&gt;<br />&nbsp;&lt;movie&gt;<br />&nbsp;&nbsp;&lt;title&gt;PHP:&nbsp;Behind&nbsp;the&nbsp;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.&nbsp;Coder&lt;/name&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;actor&gt;Onlivia&nbsp;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.&nbsp;Coder&lt;/name&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;actor&gt;El&nbsp;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,&nbsp;this&nbsp;language.&nbsp;It's&nbsp;like,&nbsp;a&nbsp;programming&nbsp;language.&nbsp;Or&nbsp;is&nbsp;it&nbsp;a<br />&nbsp;&nbsp;&nbsp;scripting&nbsp;language?&nbsp;All&nbsp;is&nbsp;revealed&nbsp;in&nbsp;this&nbsp;thrilling&nbsp;horror&nbsp;spoof<br />&nbsp;&nbsp;&nbsp;of&nbsp;a&nbsp;documentary.<br />&nbsp;&nbsp;&lt;/plot&gt;<br />&nbsp;&nbsp;&lt;great-lines&gt;<br />&nbsp;&nbsp;&nbsp;&lt;line&gt;PHP&nbsp;solves&nbsp;all&nbsp;my&nbsp;web&nbsp;problems&lt;/line&gt;<br />&nbsp;&nbsp;&lt;/great-lines&gt;<br />&nbsp;&nbsp;&lt;rating&nbsp;type="thumbs"&gt;7&lt;/rating&gt;<br />&nbsp;&nbsp;&lt;rating&nbsp;type="stars"&gt;5&lt;/rating&gt;<br />&nbsp;&lt;/movie&gt;<br />&lt;/movies&gt;<br /></span><span style="color: #007700">XML;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>   </div>  </p>  <p class="para">   The simplicity of SimpleXML appears most clearly when one extracts   a string or number from a basic XML document.   <div class="example">    <p><b>Example #2 Getting <i>&lt;plot&gt;</i></b></p>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">include&nbsp;</span><span style="color: #DD0000">'example.php'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$xml&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">SimpleXMLElement</span><span style="color: #007700">(</span><span style="color: #0000BB">$xmlstr</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #0000BB">$xml</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">movie</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]-&gt;</span><span style="color: #0000BB">plot</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;"So&nbsp;this&nbsp;language.&nbsp;It's&nbsp;like..."<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>   </div>  </p>  <p class="para">   Accessing elements within an XML document that contain characters not permitted under    PHP&#039;s naming convention (e.g. the hyphen) can be accomplished by encapsulating the   element name within braces and the apostrophe.   <div class="example">    <p><b>Example #3 Getting <i>&lt;line&gt;</i></b></p>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">include&nbsp;</span><span style="color: #DD0000">'example.php'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$xml&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">SimpleXMLElement</span><span style="color: #007700">(</span><span style="color: #0000BB">$xmlstr</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #0000BB">$xml</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">movie</span><span style="color: #007700">-&gt;{</span><span style="color: #DD0000">'great-lines'</span><span style="color: #007700">}-&gt;</span><span style="color: #0000BB">line</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;"PHP&nbsp;solves&nbsp;all&nbsp;my&nbsp;web&nbsp;problems"<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>   </div>  </p>  <p class="para">   <div class="example">    <p><b>Example #4 Accessing non-unique elements in SimpleXML</b></p>    <div class="example-contents"><p>     When multiple instances of an element exist as children of     a single parent element, normal iteration techniques apply.    </p></div>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">include&nbsp;</span><span style="color: #DD0000">'example.php'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$xml&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">SimpleXMLElement</span><span style="color: #007700">(</span><span style="color: #0000BB">$xmlstr</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;For&nbsp;each&nbsp;&lt;movie&gt;&nbsp;node,&nbsp;we&nbsp;echo&nbsp;a&nbsp;separate&nbsp;&lt;plot&gt;.&nbsp;*/<br /></span><span style="color: #007700">foreach&nbsp;(</span><span style="color: #0000BB">$xml</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">movie&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$movie</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$movie</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">plot</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'&lt;br&nbsp;/&gt;'</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>   </div>  </p>  <p class="para">   <div class="example">    <p><b>Example #5 Using attributes</b></p>    <div class="example-contents"><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" class="type array">array</a>.    </p></div>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">include&nbsp;</span><span style="color: #DD0000">'example.php'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$xml&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">SimpleXMLElement</span><span style="color: #007700">(</span><span style="color: #0000BB">$xmlstr</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;Access&nbsp;the&nbsp;&lt;rating&gt;&nbsp;nodes&nbsp;of&nbsp;the&nbsp;first&nbsp;movie.<br />&nbsp;*&nbsp;Output&nbsp;the&nbsp;rating&nbsp;scale,&nbsp;too.&nbsp;*/<br /></span><span style="color: #007700">foreach&nbsp;(</span><span style="color: #0000BB">$xml</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">movie</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]-&gt;</span><span style="color: #0000BB">rating&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$rating</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;switch((string)&nbsp;</span><span style="color: #0000BB">$rating</span><span style="color: #007700">[</span><span style="color: #DD0000">'type'</span><span style="color: #007700">])&nbsp;{&nbsp;</span><span style="color: #FF8000">//&nbsp;Get&nbsp;attributes&nbsp;as&nbsp;element&nbsp;indices<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">case&nbsp;</span><span style="color: #DD0000">'thumbs'</span><span style="color: #007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$rating</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'&nbsp;thumbs&nbsp;up'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #DD0000">'stars'</span><span style="color: #007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$rating</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'&nbsp;stars'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>   </div>  </p>  <p class="para">   <div class="example">    <p><b>Example #6 Comparing Elements and Attributes with Text</b></p>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?