Rumah > Artikel > pembangunan bahagian belakang > php生成xml和转换[源码]_PHP教程
php生成xml就像树一样,逐个添加节点,可以在一个父节点下添加多个子节点, function madexml()
<ol class="dp-c"> <li class="alt"><span><span>{ </span></span></li> <li> <span> </span><span class="comment">//获取模板信息 </span><span> </span> </li> <li class="alt"> <span> </span><span class="vars">$strTempInfo</span><span> = </span><span class="vars">$this</span><span>->modelCmsObj->getTemplate(</span><span class="string">2007</span><span>); </span> </li> <li> <span> </span><span class="vars">$arrTemp</span><span> = </span><span class="func">explode</span><span>(</span><span class="string">"#"</span><span>,</span><span class="vars">$strTempInfo</span><span>); </span> </li> <li class="alt"> <span> </span><span class="func">array_shift</span><span>(</span><span class="vars">$arrTemp</span><span>); </span> </li> <li> <span> </span><span class="vars">$arrContents</span><span> = </span><span class="keyword">array</span><span>(); </span> </li> <li class="alt"> <span> </span><span class="keyword">foreach</span><span>(</span><span class="vars">$arrTemp</span><span> </span><span class="keyword">as</span><span> </span><span class="vars">$k</span><span>=></span><span class="vars">$v</span><span>) </span> </li> <li><span> { </span></li> <li class="alt"> <span> </span><span class="vars">$arrContents</span><span>[</span><span class="vars">$k</span><span>]=</span><span class="func">explode</span><span>(</span><span class="string">","</span><span>,</span><span class="vars">$v</span><span>); </span> </li> <li><span> } </span></li> <li class="alt"> <span> </span><span class="comment">//解析为xml文件 </span><span> </span> </li> <li> <span> </span><span class="vars">$objDom</span><span> = </span><span class="keyword">new</span><span> DOMDocument(</span><span class="string">"1.0"</span><span>); </span> </li> <li class="alt"> <span> header(</span><span class="string">"Content-Type: text/plain"</span><span>); </span> </li> <li> <span> </span><span class="comment">//添加元素和文本节点 </span><span> </span> </li> <li class="alt"> <span> </span><span class="vars">$root</span><span> = </span><span class="vars">$objDom</span><span>->createElement(</span><span class="string">"recommend"</span><span>); </span> </li> <li> <span> </span><span class="vars">$objDom</span><span>->appendChild(</span><span class="vars">$root</span><span>); </span> </li> <li class="alt"> <span> </span><span class="keyword">foreach</span><span>(</span><span class="vars">$arrContents</span><span> </span><span class="keyword">as</span><span> </span><span class="vars">$k</span><span>=></span><span class="vars">$v</span><span>) </span> </li> <li><span> { </span></li> <li class="alt"> <span> </span><span class="vars">$item</span><span> = </span><span class="vars">$objDom</span><span>->createElement(</span><span class="string">"entry"</span><span>); </span> </li> <li> <span> </span><span class="vars">$root</span><span>->appendChild(</span><span class="vars">$item</span><span>); </span> </li> <li class="alt"> <span> </span><span class="vars">$nextitem1</span><span> = </span><span class="vars">$objDom</span><span>->createElement(</span><span class="string">"simgurl"</span><span>); </span> </li> <li> <span> </span><span class="vars">$nextitem2</span><span> = </span><span class="vars">$objDom</span><span>->createElement(</span><span class="string">"imgurl"</span><span>); </span> </li> <li class="alt"> <span> </span><span class="vars">$nextitem3</span><span> = </span><span class="vars">$objDom</span><span>->createElement(</span><span class="string">"fileurl"</span><span>); </span> </li> <li> <span> </span><span class="vars">$item</span><span>->appendChild(</span><span class="vars">$nextitem1</span><span>); </span> </li> <li class="alt"> <span> </span><span class="vars">$item</span><span>->appendChild(</span><span class="vars">$nextitem2</span><span>); </span> </li> <li> <span> </span><span class="vars">$item</span><span>->appendChild(</span><span class="vars">$nextitem3</span><span>); </span> </li> <li class="alt"> <span> </span><span class="vars">$text1</span><span> = </span><span class="vars">$objDom</span><span>->createTextNode(</span><span class="vars">$v</span><span>[0]); </span> </li> <li> <span> </span><span class="vars">$text</span><span> = </span><span class="vars">$objDom</span><span>->createTextNode(</span><span class="vars">$v</span><span>[1]); </span> </li> <li class="alt"> <span> </span><span class="vars">$text2</span><span> = </span><span class="vars">$objDom</span><span>->createTextNode(</span><span class="vars">$v</span><span>[2]); </span> </li> <li> <span> </span><span class="vars">$nextitem2</span><span>->appendChild(</span><span class="vars">$text1</span><span>); </span> </li> <li class="alt"> <span> </span><span class="vars">$nextitem3</span><span>->appendChild(</span><span class="vars">$text2</span><span>); </span> </li> <li> <span> </span><span class="vars">$nextitem1</span><span>->appendChild(</span><span class="vars">$text</span><span>); </span> </li> <li class="alt"><span> } </span></li> <li><span> </span></li> <li class="alt"> <span> </span><span class="func">echo</span><span> </span><span class="vars">$objDom</span><span>->saveXML(); </span> </li> <li><span>} </span></li> </ol>
<ol class="dp-c"> <li class="alt"><span><span><?php </span></span></span></li> <li> <span class="vars">$string</span><span> = <span class="comment">//声明xml文档 </span><span> </span></span> </li> <li class="alt"><span><?xml version=</span><span class="string">1.0</span><span>?> </span></span></li> <li><span><document> </document></span></li> <li class="alt"><span><title>Forty What?</title> </span></li> <li><span><from>Joe</from> </span></li> <li class="alt"><span><to>Jane</to> </span></li> <li><span> </span></li> <li class="alt"> <span> I know that</span><span class="string">s the answer -- but what</span><span>s the question? </span> </li> <li><span> </span></li> <li class="alt"><span> </span></li> <li><span>XML; </span></li> <li class="alt"><span> </span></li> <li> <span class="vars">$xml</span><span> = simplexml_load_string(</span><span class="vars">$string</span><span>); </span> </li> <li class="alt"><span> </span></li> <li> <span>var_dump(</span><span class="vars">$xml</span><span>); </span> </li> <li class="alt"><span>?> </span></li> <li><span> </span></li> <li class="alt"><span> </span></li> <li> <span>This script will display: </span> <p align="left"></p> <div style="display:none;"> <span id="url" itemprop="url">http://www.bkjia.com/PHPjc/486093.html</span><span id="indexUrl" itemprop="indexUrl">www.bkjia.com</span><span id="isOriginal" itemprop="isOriginal">true</span><span id="isBasedOnUrl" itemprop="isBasedOnUrl">http://www.bkjia.com/PHPjc/486093.html</span><span id="genre" itemprop="genre">TechArticle</span><span id="description" itemprop="description">php生成xml就像树一样,逐个添加节点,可以在一个父节点下添加多个子节点, function madexml() { //获取模板信息 $strTempInfo = $this -modelCmsObj-g...</span> </div> <div class="art_confoot"></div> </li> </ol>