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

PHP输出生成XML文件实例程序

May 25, 2016 pm 04:42 PM
php output xml Generate xml file

在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(); 
}
Copy after login

方法二,代码如下:

<?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>"; 
?>
Copy after login

方法三:使用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 
?>
Copy after login

方法四: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/>
Copy after login


文章网址:

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

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

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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)