溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

php數(shù)組怎么轉(zhuǎn)換為xml形式

發(fā)布時(shí)間:2022-05-13 14:06:08 來(lái)源:億速云 閱讀:171 作者:iii 欄目:大數(shù)據(jù)

本文小編為大家詳細(xì)介紹“php數(shù)組怎么轉(zhuǎn)換為xml形式”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“php數(shù)組怎么轉(zhuǎn)換為xml形式”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來(lái)學(xué)習(xí)新知識(shí)吧。

1、說(shuō)明

XML 是可擴(kuò)展標(biāo)記語(yǔ)言(EXtensible Markup Language)。

XML 是一種很像HTML的標(biāo)記語(yǔ)言。

XML 的設(shè)計(jì)宗旨是傳輸數(shù)據(jù),而不是顯示數(shù)據(jù)。

XML 標(biāo)簽沒(méi)有被預(yù)定義。您需要自行定義標(biāo)簽。

XML 被設(shè)計(jì)為具有自我描述性。

XML 是 W3C 的推薦標(biāo)準(zhǔn)。

2、轉(zhuǎn)換實(shí)例

<?php
namespace Library;
/**
 * 最外層的當(dāng)個(gè)元素可以自定義標(biāo)簽,內(nèi)層單個(gè)元素統(tǒng)一標(biāo)簽為$defaultSingleOuter
 */
class XML
{
    private static $version = "1.0";
    private static $encoding = 'utf-8';
    // 最外層
    private static $outer = '';
    // 最外層屬性
    private static $outerAttribut = [];
    // 單個(gè)元素的外層
    private static $singleOuter = '';
    // 單個(gè)元素外層的屬性
    private static $singleOuterAttribut = [];
 
    private static $defaultSingleOuter = 'item';
 
    public function A2XML(array $data)
    {
        $xml = new \XMLWriter();
        $this->begin($xml);
 
        // 寫(xiě)數(shù)據(jù)
        if (is_numeric(current(array_keys($data)))) {
            foreach ($data as $key => $val) {
                $this->singleBegin($xml, true);
                $this->writeElement($xml, $val);
                $this->singleEnd($xml);
            }
        } else {
            $this->writeElement($xml, $data);
        }
 
        return $this->end($xml);
    }
 
    /**
     * 寫(xiě)數(shù)據(jù)
     *
     * @param \XMLWriter $xml
     * @param $data
     */
    private function writeElement(\XMLWriter &$xml, $data)
    {
        if (!is_array($data)) {
            $xml->writeElement(self::$defaultSingleOuter, $data);
            return;
        }
        foreach ($data as $key => $val) {
            if (is_numeric($key)) {
                is_array($val) && $this->singleBegin($xml);
                $this->writeElement($xml, $val);
                is_array($val) && $this->singleEnd($xml);
                continue;
            }
 
            if (is_array($val)) {
                $xml->startElement($key);
                $this->writeElement($xml, $val);
                $xml->endElement();
                continue;
            }
            $xml->writeElement($key, $val);
        }
    }
 
    /**
     * 開(kāi)始
     *
     * @param \XMLWriter $xml
     */
    private function begin(\XMLWriter &$xml)
    {
        $xml->openMemory();
        $xml->startDocument(self::$version, self::$encoding);
 
        if (self::$outer) {
            $xml->startElement(self::$outer);
        }
 
        if (self::$outerAttribut) {
            foreach (self::$outerAttribut as $key => $val) {
                $xml->writeAttribute($key, $val);
            }
        }
    }
 
    /**
     * 結(jié)束
     *
     * @param \XMLWriter $xml
     * @return string
     */
    private function end(\XMLWriter $xml)
    {
        if (self::$outer) {
            $xml->endElement();
        }
 
        $xml->endDocument();
 
        header("Content-type: text/xml");
        //取得緩沖區(qū)里的xml字符串
        return $xml->outputMemory(true);
    }
 
    /**
     * 單個(gè)元素的開(kāi)始
     *
     * @param \XMLWriter $xml
     * @param bool $first
     */
    private function singleBegin(\XMLWriter $xml, $first = false)
    {
        if ($first) {
            $xml->startElement(self::$singleOuter ?: self::$defaultSingleOuter);
 
            if (self::$singleOuterAttribut) {
                foreach (self::$singleOuterAttribut as $key => $val) {
                    $xml->writeAttribute($key, $val);
                }
            }
        } else {
            $xml->startElement(self::$defaultSingleOuter);
        }
    }
 
    /**
     * 單個(gè)元素的結(jié)束
     *
     * @param \XMLWriter $xml
     */
    private function singleEnd(\XMLWriter $xml)
    {
        $xml->endElement();
    }
 
    public function setVersion($version = 1.0)
    {
        self::$version = $version;
        return $this;
    }
 
    public function setEncoding($encoding = 'utf-8')
    {
        self::$version = $encoding;
        return $this;
    }
 
    public function setOuter($outer = '')
    {
        self::$outer = $outer;
        return $this;
    }
 
    public function setOuterAttribut(array $outerAttribut = [])
    {
        self::$outerAttribut = $outerAttribut;
        return $this;
    }
 
    public function setSingleOuter($singleOuter)
    {
        self::$singleOuter = $singleOuter;
        return $this;
    }
 
    public function setSingleOuterAttribut(array $singleOuterAttribut = [])
    {
        self::$singleOuterAttribut = $singleOuterAttribut;
        return $this;
    }
 
    public function setDefaultSingleOuter($defaultSingleOuter)
    {
        self::$defaultSingleOuter = $defaultSingleOuter;
        return $this;
    }
}

讀到這里,這篇“php數(shù)組怎么轉(zhuǎn)換為xml形式”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過(guò)才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI