0
這個XML文件是我到目前爲止有:創建使用simplexml_load_string,的addChild和的addAttribute
這是我收到的輸出:http://i.imgur.com/mGRAJPe.gif
似乎所有的子節點都成爲一個孩子第一個datafield元素,而不是嵌套在它們各自的數據字段父項中。有什麼建議麼?
$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);
}
}
}
非常感謝,這完美的工作! – rtd1123 2013-02-12 16:55:41