如何将带有属性的多维数组保存到 xml 文件


How to save multidimensional array with attributes to xml file

这个具有属性的多维数组:

Array
(
    [EPaperPage] => Array
        (
            [@attributes] => Array
                (
                    [ePdfPageSize] => 1
                    [eJpegPageSize] => 0
                )
            [EDayOfPub] => Array
                (
                    [@attributes] => Array
                        (
                            [dtmCreatedDate] => 2016-02-02T14:40:01
                            [dtmLastUpdate] => 2016-02-02T14:40:01
                            [strCreatorLoginName] => sdsdt
                            [strLastUpdaterLoginName] => sdsdst
                            [nContentVersion] => 3
                        )
                    [OrigId] => Array
                        (
                            [@attributes] => Array
                                (
                                    [kLocationId] => 960
                                    [strPubCustId] => OND
                                    [eLogType] => 20
                                    [strDocId] => fd34c3c5-053b-45e7-bd92-66f04db013aa
                                    [nVersion] => 0
                                )
                        )
[...]

必须像这样保存在文件中:

<?xml version="1.0" encoding="UTF-8"?>
    <DialogEPaperPage>
        <EPaperPage ePdfPageSize="1" eJpegPageSize="0">
            <EDayOfPub dtmCreatedDate="2016-02-02T14:40:01" dtmLastUpdate="2016-02-02T14:40:01" strCreatorLoginName="sdfsdf" [...]

您可以使用递归函数遍历数组。另外,我建议您使用SimpleXML库,因为它比DOM更简单。所以生成代码是:

<?php    
function build($value, $node) {
    if (is_array($value) == true) {
        foreach ($value as $key => $element) {
            if ($key == '@attributes') {
                foreach ($element as $n => $v) {
                    $node->addAttribute($n, $v);
                }
            } else {
                $newNode = $node->addChild($key);
                build($element, $newNode);
            }
        }
    } else {
        $node->nodeValue = $value;
    }
}
$node = new SimpleXMLElement('<root/>');
build($test_array, $node);
$node->asXML('/tmp/filename.xml');
?>

对于给定的数组:

$test_array = [
    "EPaperPage" => [
        "@attributes" => [
                "ePdfPageSize" => 1,
                "eJpegPageSize" => 0
        ],
        "EDayOfPub" => [
            "@attributes" => [
                    "dtmCreatedDate" => "2016-02-02T14:40:01",
                    "dtmLastUpdate" => "2016-02-02T14:40:01",
                    "strCreatorLoginName" => "sdsdt",
                    "strLastUpdaterLoginName" => "sdsdst",
                    "nContentVersion" => 3
                ],
            "OrigId" => [
                    "@attributes" => [
                            "kLocationId" => "960",
                            "strPubCustId" => "OND",
                            "eLogType" => "20",
                            "strDocId" => "fd34c3c5-053b-45e7-bd92-66f04db013aa",
                            "nVersion" => 0
                    ]
            ]
        ]
    ]
];

它生成以下 XML:

<?xml version="1.0"?>
<root>
    <EPaperPage ePdfPageSize="1" eJpegPageSize="0">
        <EDayOfPub dtmCreatedDate="2016-02-02T14:40:01" dtmLastUpdate="2016-02-02T14:40:01" strCreatorLoginName="sdsdt" strLastUpdaterLoginName="sdsdst" nContentVersion="3">
            <OrigId kLocationId="960" strPubCustId="OND" eLogType="20" strDocId="fd34c3c5-053b-45e7-bd92-66f04db013aa" nVersion="0"/>
        </EDayOfPub>
    </EPaperPage>
</root>
您可以使用

DOMDocument类:

/* Create new XML document: */
$xml = new DOMDocument( '1.0', 'utf-8' );
/* Add DialogEPaperPage child: */
$xml->appendChild( $root = $xml->createElement( 'DialogEPaperPage' ) );
/* Add EPaperPage child: */
$root->appendChild( $EPaperPage = $xml->createElement( 'EPaperPage' ) );
/* Set EPaperPage child attributes: */
$EPaperPage->setAttribute( 'ePdfPageSize', 1 );
$EPaperPage->setAttribute( 'eJpegPageSize', 0 );
/* Add EDayOfPub child: */
$EPaperPage->appendChild( $EDayOfPub = $xml->createElement( 'EDayOfPub' ) );
/* Set EDayOfPub child attributes: */
$EDayOfPub->setAttribute( 'dtmCreatedDate', '2016-02-02T14:40:01' );
$EDayOfPub->setAttribute( 'dtmLastUpdate', '2016-02-02T14:40:01' );
$EDayOfPub->setAttribute( 'strCreatorLoginName', 'sdsdt' );
$EDayOfPub->setAttribute( 'strLastUpdaterLoginName', 'sdsdt' );
$EDayOfPub->setAttribute( 'nContentVersion', '3' );
(...)
/* Set option to pretty output: */
$xml->formatOutput = True;
/* Echoes XML: */  
$xml->saveXML());

查看更多关于 DOMDocument 的信息

<小时 />

(编辑:更正了代码中的错误)