0

This is what I have so far:

This is the output I am receiving: http://i.imgur.com/mGRAJPe.gif

It seems all of the subfield nodes are becoming a child of the 1st datafield element, instead of being nested in their respective datafield parent. Any suggestions?

$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);
        }
    }
}

1 Answer 1

1

Well, that's what you do here, you handle datafield as a single node, so the first one is used (why should it be the last one?):

$xml->record->datafield->addChild('subfield', $value);

But you already have your desired parent in $addData, just use it:

$addData->addChild('subfield', $value);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.