Home  >  Article  >  Backend Development  >  PHP generates xml and converts [source code]_PHP tutorial

PHP generates xml and converts [source code]_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:41:51848browse

PHP generates xml just like a tree, adding nodes one by one. You can add multiple child nodes under a parent node, 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>

simplexml_load_string converts xml string into string

<ol class="dp-c">
<li class="alt"><span><span><?php </span></span></li><li><span class="vars">$string</span><span> = <<<XML </span><span class="comment">//Declaration xml document</span><span> </span></li><li class="alt"><span><?xml version=</span><span class="string">1.0</span><span>?&gt ; </span></li><li><span><document> </span></span></li>
<li class="alt">
<span><title>Forty What?</title> </span><from>Joe</from> </li>
<li>
<span></span><to>Jane</to> 🎜></li>
<li class="alt"><span> I know that</span></li>s the answer -- but what<li><span>s the question? </span></li>
<li class="alt">
<span>&lt ;/body> </span><span class="string"></span><span></document> </span>
</li>
<li><span>XML; </span></li>
<li class="alt">$xml<span></span> = simplexml_load_string(</li>
<li>$string<span></span>); </li>
<li class="alt">
<span></span> </li>
<li>
<span class="vars"></span>var_dump(<span></span>$xml<span class="vars"></span>); <span></span>
</li>
<li class="alt">?> <span></span>
</li>
<li> <span></span><span class="vars"></span> <span></span>
</li>
<li class="alt">This script will display: <span></span>
</li>
<li><span>http://www.bkjia.com/PHPjc/486093.html</span></li>www.bkjia.com<li class="alt"><span>true</span></li>http: //www.bkjia.com/PHPjc/486093.html<li>
<span>TechArticle</span>php generates xml just like a tree, adding nodes one by one, you can add multiple child nodes under a parent node, function madexml() { //Get template information $strTempInfo = $this -modelCmsObj-g...<p align="left"></p>
<div style="display:none;">
<span id="url" itemprop="url"></span>
<span id="indexUrl" itemprop="indexUrl"></span>
</div>
</li>
</ol>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn