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"><?php<br />$xmlstr </span><span style="color: #007700">= <<<XML<br /></span><span style="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 /> <great-lines><br /> <line>PHP solves all my web problems</line><br /> </great-lines><br /> <rating type="thumbs">7</rating><br /> <rating type="stars">5</rating><br /> </movie><br /></movies><br /></span><span style="color: #007700">XML;<br /></span><span style="color: #0000BB">?></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><plot></i></b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #007700">include </span><span style="color: #DD0000">'example.php'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$xml </span><span style="color: #007700">= new </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 </span><span style="color: #0000BB">$xml</span><span style="color: #007700">-></span><span style="color: #0000BB">movie</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]-></span><span style="color: #0000BB">plot</span><span style="color: #007700">; </span><span style="color: #FF8000">// "So this language. It's like..."<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> <p class="para"> Accessing elements within an XML document that contain characters not permitted under PHP'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><line></i></b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #007700">include </span><span style="color: #DD0000">'example.php'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$xml </span><span style="color: #007700">= new </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 </span><span style="color: #0000BB">$xml</span><span style="color: #007700">-></span><span style="color: #0000BB">movie</span><span style="color: #007700">->{</span><span style="color: #DD0000">'great-lines'</span><span style="color: #007700">}-></span><span style="color: #0000BB">line</span><span style="color: #007700">; </span><span style="color: #FF8000">// "PHP solves all my web problems"<br /></span><span style="color: #0000BB">?></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"><?php<br /></span><span style="color: #007700">include </span><span style="color: #DD0000">'example.php'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$xml </span><span style="color: #007700">= new </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">/* For each <movie> node, we echo a separate <plot>. */<br /></span><span style="color: #007700">foreach (</span><span style="color: #0000BB">$xml</span><span style="color: #007700">-></span><span style="color: #0000BB">movie </span><span style="color: #007700">as </span><span style="color: #0000BB">$movie</span><span style="color: #007700">) {<br /> echo </span><span style="color: #0000BB">$movie</span><span style="color: #007700">-></span><span style="color: #0000BB">plot</span><span style="color: #007700">, </span><span style="color: #DD0000">'<br />'</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?></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"><?php<br /></span><span style="color: #007700">include </span><span style="color: #DD0000">'example.php'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$xml </span><span style="color: #007700">= new </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">/* Access the <rating> nodes of the first movie.<br /> * Output the rating scale, too. */<br /></span><span style="color: #007700">foreach (</span><span style="color: #0000BB">$xml</span><span style="color: #007700">-></span><span style="color: #0000BB">movie</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]-></span><span style="color: #0000BB">rating </span><span style="color: #007700">as </span><span style="color: #0000BB">$rating</span><span style="color: #007700">) {<br /> switch((string) </span><span style="color: #0000BB">$rating</span><span style="color: #007700">[</span><span style="color: #DD0000">'type'</span><span style="color: #007700">]) { </span><span style="color: #FF8000">// Get attributes as element indices<br /> </span><span style="color: #007700">case </span><span style="color: #DD0000">'thumbs'</span><span style="color: #007700">:<br /> echo </span><span style="color: #0000BB">$rating</span><span style="color: #007700">, </span><span style="color: #DD0000">' thumbs up'</span><span style="color: #007700">;<br /> break;<br /> case </span><span style="color: #DD0000">'stars'</span><span style="color: #007700">:<br /> echo </span><span style="color: #0000BB">$rating</span><span style="color: #007700">, </span><span style="color: #DD0000">' stars'</span><span style="color: #007700">;<br /> break;<br /> }<br />}<br /></span><span style="color: #0000BB">?></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 + -
显示快捷键?