这是我到目前为止所拥有的:
这是我收到的输出:http: //i.imgur.com/mGRAJPe.gif
似乎所有子字段节点都成为第一个数据字段元素的子节点,而不是嵌套在各自的数据字段父节点中。有什么建议么?
$str = '<?xml version="1.0" encoding="UTF-8"?><collection xmlns="http://www.loc.gov/MARC21/slim"><record></record></collection>';
$xml = simplexml_load_string($str);
$xml->record = "";
$xml->record->addChild('leader', $data["controlfield"]["000"]["40"]);
foreach ($data["controlfield"] as $dcf => $value) {
foreach ($value as $dcfv) {
$addControl = $xml->record->addChild('controlfield', $dcfv);
$addControl->addAttribute('tag', $dcf);
}
}
foreach ($data["datafield"] as $dftc => $dfarray) {
$addData = $xml->record->addChild('datafield');
$addData->addAttribute('tag', $dftc);
$addData->addAttribute('ind1', $dfarray['ind1']);
$addData->addAttribute('ind2', $dfarray['ind2']);
foreach ($dfarray as $code => $value) {
if(($code != "ind1") && ($code != "ind2")) {
$addSubs = $xml->record->datafield->addChild('subfield', $value);
$addSubs->addAttribute('code', $code);
}
}
}