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

时间:2016-02-03 09:17:44

标签: php xml multidimensional-array

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

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" [...]

2 个答案:

答案 0 :(得分:0)

您可以使用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());

eval.in demo

详细了解DOMDocument

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

答案 1 :(得分:0)

您可以使用递归函数遍历数组。另外,我建议你使用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>