这个具有属性的多维数组:
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
(编辑:更正了代码中的错误)