我有像下面的php数组
$row = array (
'name' => 'david',
'bio' => 'good man'
);
我想将此数组转换为相应的XML页面,如下面的场景。
<?xml version="1.0" encoding="UTF-8"?>
<note>
<name>david</name>
<bio>good man</bio>
</note>
答案 0 :(得分:1)
尝试这样。
$xml = new SimpleXMLElement('<note/>');
$row = array_flip($row);
array_walk_recursive($row, array ($xml, 'addChild'));
echo htmlentities($xml->asXML());
答案 1 :(得分:0)
使用DOM生成XML:
$row = array (
'name' => 'david',
'bio' => 'good man'
);
$dom = new DOMDocument();
$dom->formatOutput = TRUE;
$note = $dom->appendChild($dom->createElement('note'));
foreach ($row as $name => $value) {
$note
->appendChild($dom->createElement($name))
->appendChild($dom->createTextNode($value));
}
echo $dom->saveXml();
输出:
<?xml version="1.0"?>
<note>
<name>david</name>
<bio>good man</bio>
</note>