Heim >Backend-Entwicklung >PHP-Tutorial >Über die Verwendung von XML in PHP (SEO)
Bezüglich der Verwendung von XML enthält dieser Ort zwei Funktionen, die nur als Referenz dienen. Wenn Sie Fragen haben, können Sie die Dokumentation nachschlagen.
<code><span><span>function</span><span>edit_xml_file</span><span>(<span>$xml_file_path</span>,<span>$datas</span>,<span>$is_index</span> = false,<span>$is_compress</span> = true)</span>{</span><span>$doc</span> = <span>new</span> DomDocument(<span>'1.0'</span>, <span>'utf-8'</span>); <span>$doc</span>->formatOutput = <span>true</span>; <span>$flag</span> = <span>false</span>; <span>if</span>( !@file_exists(<span>$xml_file_path</span>) || !<span>$doc</span>->load(<span>$xml_file_path</span>)) { <span>$flag</span> = create_xml_file(<span>$xml_file_path</span>,<span>$datas</span>,<span>$is_index</span>,<span>$is_compress</span>); <span>return</span><span>$flag</span>; } <span>//读取文件数据</span><span>$xmldata</span> = simplexml_load_file(<span>$xml_file_path</span>); <span>$newxmldata</span> = <span>array</span>(); <span>$xmL_url</span> = <span>array</span>(); <span>foreach</span> (<span>$xmldata</span><span>as</span><span>$key</span>=><span>$obj</span>){ <span>$obj</span> = (<span>array</span>) <span>$obj</span>; <span>if</span>(<span>isset</span>(<span>$obj</span>[<span>'lastmod'</span>])){ <span>$obj</span>[<span>'lastmod'</span>] = date(<span>'c'</span>,time()); } <span>$xmL_url</span>[] = <span>$obj</span>[<span>'loc'</span>]; <span>$newxmldata</span>[][<span>$key</span>] = <span>$obj</span>; } <span>//将新添加的数据放在一起</span><span>foreach</span> (<span>$datas</span><span>as</span><span>$data</span>){ <span>if</span>(!in_array(<span>$data</span>[<span>$key</span>][<span>'loc'</span>], <span>$xmL_url</span>)){ <span>$newxmldata</span>[] = <span>$data</span>; } } <span>//根据新数据重新更新创建文件</span><span>if</span>(!<span>empty</span>(<span>$newxmldata</span>) && unlink(<span>$xml_file_path</span>)){ <span>$flag</span> = create_xml_file(<span>$xml_file_path</span>,<span>$newxmldata</span>,<span>$is_index</span>,<span>$is_compress</span>); } <span>unset</span>(<span>$datas</span>); <span>return</span><span>$flag</span>; } <span>/** xml,追加数据 *<span> @param</span> xml_file_path 文件路径,$data 数据,$is_index 为了区分是地图文件还是地图索引,默认是地图文件 *<span> @author</span> zdj *<span> @date</span> 2015-01-23 */</span><span><span>function</span><span>add_data_xml_file</span><span>(<span>$xml_file_path</span>,<span>$datas</span>,<span>$is_index</span> = false,<span>$is_compress</span> = true)</span>{</span><span>$doc</span> = <span>new</span> DomDocument(<span>'1.0'</span>, <span>'utf-8'</span>); <span>$doc</span>->formatOutput = <span>true</span>; <span>$flag</span> = <span>false</span>; <span>if</span>( !@file_exists(<span>$xml_file_path</span>) || !<span>$doc</span>->load(<span>$xml_file_path</span>)) { <span>$flag</span> = create_xml_file(<span>$xml_file_path</span>,<span>$datas</span>,<span>$is_index</span>,<span>$is_compress</span>); <span>return</span><span>$flag</span>; } <span>$root</span> = <span>$doc</span>->documentElement; <span>foreach</span> (<span>$datas</span><span>as</span><span>$data</span>){ <span>foreach</span> (<span>$data</span><span>as</span><span>$key</span>=><span>$obj</span>){ <span>$key_dom</span> = <span>$doc</span>->createElement(<span>$key</span>); <span>foreach</span> (<span>$obj</span><span>as</span><span>$attribute</span> =><span>$attribute_value</span>){ <span>$attribute_dom</span> = <span>$doc</span>->createElement(<span>$attribute</span>); <span>$attribute_dom_value</span> = <span>$doc</span>->createTextNode(<span>$attribute_value</span>); <span>$attribute_dom</span>->appendChild(<span>$attribute_dom_value</span>); <span>$key_dom</span>->appendChild(<span>$attribute_dom</span>); } <span>$root</span>->appendChild(<span>$key_dom</span>); } } <span>$doc</span>->appendChild(<span>$root</span>); <span>$doc</span>->save(<span>$xml_file_path</span>); <span>//创建压缩的文件</span><span>$xml_gz_file_path</span> = <span>$xml_file_path</span>.<span>'.gz'</span>; <span>if</span>(<span>$is_compress</span>){ <span>$fp</span> = gzopen (<span>$xml_gz_file_path</span>, <span>'w9'</span>); gzwrite (<span>$fp</span>, <span>$doc</span>->saveXML()); gzclose(<span>$fp</span>); } <span>//给文件赋予权限//给文件赋予权限</span><span>if</span>(file_exists(<span>$xml_file_path</span>)){ chmod(<span>$xml_file_path</span>, <span>0744</span>); } <span>if</span>(file_exists(<span>$xml_gz_file_path</span>)){ chmod(<span>$xml_gz_file_path</span>, <span>0744</span>); } <span>unset</span>(<span>$datas</span>); }</code>
Urheberrechtserklärung: Dieser Artikel ist ein Originalartikel des Bloggers und darf nicht ohne die Erlaubnis des Bloggers reproduziert werden.
Das Obige stellt die Verwendung von XML in PHP (SEO) vor, einschließlich Aspekten des Inhalts. Ich hoffe, dass es für Freunde hilfreich sein wird, die sich für PHP-Tutorials interessieren.