开发者

How do I append XML together in PHP

开发者 https://www.devze.com 2023-03-31 15:09 出处:网络
Whats not known how to do properly is the following... $attendXml = \"\"; for ($i=0;$i<count($attendData);$i++) {

Whats not known how to do properly is the following...

$attendXml = "";
for ($i=0;$i<count($attendData);$i++) {
    $attendXml += assocArrayToXML('row',$attendData[$i]);
}

I have written wrong but I think you see what I am trying to do, the code comes from the following program. Retrieving the organXml works okay, the problem occurs with an array (none associative) containing a number of (associative arrays) that's the problem.

How do I merge th开发者_开发百科e XML of each of the associative arrays into one XML differentiated by 'row'.

function assocArrayToXML($root_element_name,$ar)
{
    $xml = new SimpleXMLElement("<?xml version=\"1.0\"?><{$root_element_name}>        </{$root_element_name}>");
    $f = create_function('$f,$c,$a','
            foreach($a as $k=>$v) {
            if(is_array($v)) {
                $ch=$c->addChild($k);
                $f($f,$ch,$v);
            } else {
                $c->addChild($k,$v);
            }
        }');
$f($f,$xml,$ar);
return $xml->asXML();
} 

// Include Libraries
include('services\OrganisationService.php'); 
include('services\AttendeeService.php');

// Target Organisation
$organ_id = 1;

// Read Organisation Data 
$organServ = new OrganisationService();
$organData = $organServ->getOrganisationByID($organ_id);
$organXml = assocArrayToXML('organisation',$organData);

// Read Attendees Data (For Organisation)
$attendServ = new AttendeeService();
$attendData = $attendServ->getAllActiveAttendeeByOrg($organ_id);

$attendXml = "";
for ($i=0;$i<count($attendData);$i++) {
    $attendXml += assocArrayToXML('row',$attendData[$i]);
}

//var_dump($attendData);
header ("Content-Type:text/xml");
echo $attendXml;
?>


You need to separate the creation of a nodes in the tree based on an associative array from the creation of the entire xml document string. I also recommend against using create_function to define a recursive function, and instead consider creating a class for handling the XML rendering.

0

精彩评论

暂无评论...
验证码 换一张
取 消