PHP generates XML document source code_PHP tutorial

WBOY
Release: 2016-07-13 17:40:24
Original
898 people have browsed it

Generate XML source code:

<span style="color: rgb(0, 0, 0);">    $dom <span style="color: rgb(0, 0, 204);">=</span> new DOMDocument<span style="color: rgb(0, 0, 204);">(</span><span style="color: rgb(0, 0, 204);">)</span><span style="color: rgb(0, 0, 204);">;</span><br>     if<span style="color: rgb(0, 0, 204);">(</span><span style="color: rgb(0, 0, 204);">!</span><span style="color: rgb(0, 0, 204);">$</span>dom<span style="color: rgb(0, 0, 204);">)</span><br>     <span style="color: rgb(0, 0, 204);">{</span><br>         printf<span style="color: rgb(0, 0, 204);">(</span><span style="color: rgb(255, 0, 255);">"构造XML失败! "</span><span style="color: rgb(0, 0, 204);">)</span><span style="color: rgb(0, 0, 204);">;</span><br>         return <span style="color: rgb(0, 0, 204);">-</span>3999<span style="color: rgb(0, 0, 204);">;</span><br>     <span style="color: rgb(0, 0, 204);">}</span><br> <br>     if<span style="color: rgb(0, 0, 204);">(</span>FALSE <span style="color: rgb(0, 0, 204);">=</span><span style="color: rgb(0, 0, 204);">=</span> <span style="color: rgb(0, 0, 204);">(</span><span style="color: rgb(0, 0, 204);">$</span>dom<span style="color: rgb(0, 0, 204);">-</span><span style="color: rgb(0, 0, 204);">></span>load<span style="color: rgb(0, 0, 204);">(</span><span style="color: rgb(0, 0, 204);">$</span>filename<span style="color: rgb(0, 0, 204);">)</span><span style="color: rgb(0, 0, 204);">)</span><span style="color: rgb(0, 0, 204);">)</span><br>     <span style="color: rgb(0, 0, 204);">{</span><br>         add_root<span style="color: rgb(0, 0, 204);">(</span><span style="color: rgb(0, 0, 204);">)</span><span style="color: rgb(0, 0, 204);">;</span><br>         if<span style="color: rgb(0, 0, 204);">(</span>FALSE <span style="color: rgb(0, 0, 204);">=</span><span style="color: rgb(0, 0, 204);">=</span> <span style="color: rgb(0, 0, 204);">(</span><span style="color: rgb(0, 0, 204);">$</span>dom<span style="color: rgb(0, 0, 204);">-</span><span style="color: rgb(0, 0, 204);">></span>load<span style="color: rgb(0, 0, 204);">(</span><span style="color: rgb(0, 0, 204);">$</span>filename<span style="color: rgb(0, 0, 204);">)</span><span style="color: rgb(0, 0, 204);">)</span><span style="color: rgb(0, 0, 204);">)</span><br>         <span style="color: rgb(0, 0, 204);">{</span><br>             return <span style="color: rgb(0, 0, 204);">-</span>3999<span style="color: rgb(0, 0, 204);">;</span><br>         <span style="color: rgb(0, 0, 204);">}</span><br>     <span style="color: rgb(0, 0, 204);">}</span><br> <br> <span style="color: rgb(0, 0, 204);">/</span><span style="color: rgb(0, 0, 204);">/reference root node </span><br> $f_node <span style="color: rgb(0, 0, 204);">=</span> $dom<span style="color: rgb(0, 0, 204);">-</span><span style="color: rgb(0, 0, 204);">></span>getElementsByTagName<span style="color: rgb(0, 0, 204);">(</span><span style="color: rgb(0, 0, 204);">$</span>parent_node<span style="color: rgb(0, 0, 204);">)</span><span style="color: rgb(0, 0, 204);">-</span><span style="color: rgb(0, 0, 204);"> ></span>item<span style="color: rgb(0, 0, 204);">(</span>0<span style="color: rgb(0, 0, 204);">)</span><span style="color: rgb(0, 0, 204);">;</span> <br> If<span style="color: rgb(0, 0, 204);">(</span><span style="color: rgb(0, 0, 204);">!</span><span style="color: rgb(0, 0, 204);">$</span>f_node<span style="color: rgb(0, 0, 204);">)</span><br>  <span style="color: rgb(0, 0, 204);">{</span><br>          $root <span style="color: rgb(0, 0, 204);">=</span> $dom<span style="color: rgb(0, 0, 204);">-</span><span style="color: rgb(0, 0, 204);">></span>getElementsByTagName<span style="color: rgb(0, 0, 204);">(</span><span style="color: rgb(255, 0, 255);">root</span><span style="color: rgb(0, 0, 204);">)</span><span style="color: rgb(0, 0, 204);">-</span><span style="color: rgb(0, 0, 204);">&gt ;</span>item<span style="color: rgb(0, 0, 204);">(</span>0<span style="color: rgb(0, 0, 204);">)</span><span style="color: rgb(0, 0, 204);">;</span> <br>                                                                                                                                                                                                                                                        $f_node <span style="color: rgb(0, 0, 204);">=</span> $dom<span style="color: rgb(0, 0, 204);">-</span><br>><span style="color: rgb(0, 0, 204);">createElement</span>(<span style="color: rgb(0, 0, 204);"></span>$<span style="color: rgb(0, 0, 204);">parent_node</span>)<span style="color: rgb(0, 0, 204);"></span>;<span style="color: rgb(0, 0, 204);"></span>           $f_node <span style="color: rgb(0, 0, 204);">=</span> $root<span style="color: rgb(0, 0, 204);">-</span><br>><span style="color: rgb(0, 0, 204);">appendChild</span>(<span style="color: rgb(0, 0, 204);"></span>$<span style="color: rgb(0, 0, 204);">f_node</span>)<span style="color: rgb(0, 0, 204);"></span>;<span style="color: rgb(0, 0, 204);"></span>  <span style="color: rgb(0, 0, 204);">}</span>  <span style="color: rgb(0, 0, 204);">        </span>/<br><span style="color: rgb(0, 0, 204);">/Create node</span><br> $item_node <span style="color: rgb(0, 0, 204);">=</span> $dom<span style="color: rgb(0, 0, 204);">-</span><br>><span style="color: rgb(0, 0, 204);">createElement</span>(<span style="color: rgb(0, 0, 204);"></span>$<span style="color: rgb(0, 0, 204);">node</span>)<span style="color: rgb(0, 0, 204);"></span>;<span style="color: rgb(0, 0, 204);"></span> $item_node <span style="color: rgb(0, 0, 204);">=</span> $f_node<span style="color: rgb(0, 0, 204);">-</span><br>><span style="color: rgb(0, 0, 204);">appendChild</span>(<span style="color: rgb(0, 0, 204);"></span>$<span style="color: rgb(0, 0, 204);">item_node</span>)<span style="color: rgb(0, 0, 204);"></span>;<span style="color: rgb(0, 0, 204);"></span> $item_value <span style="color: rgb(0, 0, 204);">=</span> $dom<span style="color: rgb(0, 0, 204);">-</span><br>><span style="color: rgb(0, 0, 204);">createTextNode</span>(<span style="color: rgb(0, 0, 204);"></span>$<span style="color: rgb(0, 0, 204);"></span>value<span style="color: rgb(0, 0, 204);"></span>)<span style="color: rgb(0, 0, 204);"></span>; <span style="color: rgb(255, 0, 0);"></span> $item_node<span style="color: rgb(0, 0, 204);">-</span><span style="color: rgb(0, 0, 204);">>> </span><p align="left"></p> <div style="display:none;"> <span id="url" itemprop="url">http://www.bkjia.com/PHPjc/486219.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/486219.html</span><span id="genre" itemprop="genre">TechArticle</span><span id="description" itemprop="description">Generate XML source code: $dom = new DOMDocument ( ) ; if ( ! $dom ) { printf ( "Construction XML failed!" ) ; return - 3999 ; } if ( FALSE == ( $ dom - load ( $ filename ) ) ) { add_root (...</span> </div></span>

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template