php生成xml和转换[源码]_PHP教程

WBOY
Lepaskan: 2016-07-13 17:41:51
asal
853 orang telah melayarinya

  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>
Salin selepas log masuk

simplexml_load_string把xml串转化为字符串

<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>
Salin selepas log masuk
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan