search
Homephp教程php手册PHP输出生成XML文件实例程序

在php中输出生成xml文件的方法有很多,有直接用header输入,也有使用DomDocument与SimpleXML实现创建xml文档的.

方法一,代码如下:

function xml_out($content, $charset = 'utf-8') { 
    @header("Expires: -1"); 
    @header("Cache-Control: no-store, private, post-check=0, pre-check=0, max-age=0", FALSE); 
    @header("Pragma: no-cache"); 
    @header("Content-type: application/xml; charset=$charset"); 
    echo &#39;<&#39; . "?xml version=\"1.0\" encoding=\"$charset\"?>\n"; 
    echo "<root><![CDATA[" . trim($content) . "]]></root>"; 
    exit(); 
}

方法二,代码如下:

<?php 
header("Content-type: text/xml"); 
echo "<?xml version=\"1.0\" encoding=\"UTF-8\""; 
echo "<users><user><name>小小菜鸟</name><age>24</age><sex>男</sex></user><user><name>艳艳</name><age>23</age><sex>女</sex></user></users>"; 
?>

方法三:使用DomDocument生成XML文件

创建节点使用createElement方法,创建文本内容使用createTextNode方法,添加子节点使用appendChild方法,创建属性使用createAttribute方法,代码如下:

<?PHP 
$data_array = array( 
    array( 
    &#39;title&#39; => &#39;title1&#39;, 
    &#39;content&#39; => &#39;content1&#39;, 
        &#39;pubdate&#39; => &#39;2009-10-11&#39;, 
    ), 
    array( 
    &#39;title&#39; => &#39;title2&#39;, 
    &#39;content&#39; => &#39;content2&#39;, 
    &#39;pubdate&#39; => &#39;2009-11-11&#39;, 
    ) 
); 
 
//  属性数组 
$attribute_array = array( 
    &#39;title&#39; => array( 
    &#39;size&#39; => 1 
    ) 
); 
 
//  创建一个XML文档并设置XML版本和编码。。 
$dom=new DomDocument(&#39;1.0&#39;, &#39;utf-8&#39;); 
 
//  创建根节点 
$article = $dom->createElement(&#39;article&#39;); 
$dom->appendchild($article); 
 
foreach ($data_array as $data) { 
    $item = $dom->createElement(&#39;item&#39;); 
    $article->appendchild($item); 
 
    create_item($dom, $item, $data, $attribute_array); 
} 
 
echo $dom->saveXML(); 
 
function create_item($dom, $item, $data, $attribute) { 
    if (is_array($data)) { 
        foreach ($data as $key => $val) { 
            //  创建元素 
            $$key = $dom->createElement($key); 
            $item->appendchild($$key); 
 
            //  创建元素值 
            $text = $dom->createTextNode($val); 
            $$key->appendchild($text); 
 
            if (isset($attribute[$key])) {  //  如果此字段存在相关属性需要设置 
                foreach ($attribute[$key] as $akey => $row) { 
                    //  创建属性节点 
                    $$akey = $dom->createAttribute($akey); 
                    $$key->appendchild($$akey); 
 
                    // 创建属性值节点 
                    $aval = $dom->createTextNode($row); 
                    $$akey->appendChild($aval); 
                }
            }   //  end if 
        } 
    }   //  end if 
}   //  end function 
?>

方法四:SimpleXML输入xml格式编码

SimpleXML作为PHP核心的组成部分,可以把XML转换为对象,但是有时候,我需要对输出的xml格式设置编码,代码如下:

$XML = new SimpleXMLElement("<foo />"); echo($XML->asXML());
 
//输出结果:<?xml version="1.0"?> <foo/>
 
//如果想输出,代码如下:<?xml version="1.0" encoding="UTF-8"?> <foo/>


文章网址:

随意转载^^但请附上教程地址。

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.