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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.