$dom
=
new
DomDocument('1.0', 'utf-8');
// 创建根节点
$article
=
$dom
->createElement('article');
$dom
->appendchild(
$article
);
foreach
(
$data_array
as
$data
) {
$item
=
$dom
->createElement('item');
$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
?>
方法 3:【XMLWriter
】
使用 XMLWriter 类创建 XML 文件,此方法在 PHP 5.1.2 后有效。另外,它可以输出多种编
码的 XML,但是输入只能是 utf-8
代码如下:
<?php
$data_array
=
array
(
array
(