开发者

XML tags closing before value being inserted

开发者 https://www.devze.com 2023-04-01 04:03 出处:网络
The following code: // Read Attendees Data (For Organisation) $attendServ = new AttendeeService(); $attendData = $attendServ->getAllActiveAttendeeByOrg($organisation_id);

The following code:

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

$attendee = new DOMDocument('1.0');
$attendee->formatOutput = true;

$root = $attendee->createElement('attendee');
$root = $attendee->appendChild($root);

for ($i=0;$i<count($attendData);$i++) {

    $row = $attendee->createElement('row');
    $row = $root->appendChild($row);

    foreach ($attendData[$i] as $tag=>$value)
    {
        $nodename = $attendee->createElement($tag);
        $nodename = $row->appendChild($nodename);

        $nodevalue = $attendee->createTextNode($v开发者_如何学Goalue);
        $nodevalue = $row->appendChild($nodevalue);
    }
}

// Test Organisation Output
header ("Content-Type:text/xml");
echo $attendee->saveXML();

Produces:

<?xml version="1.0"?>
<attendee>
  <row>
    <attendee_id/>1
    <attendee_name/>A
    <attendee_initials/>A.1.
  </row>
</attendee>

Instead of:

<?xml version="1.0"?>
<attendee>
  <row>
    <attendee_id>1</attendee_id>
    <attendee_name>A</attendee_name>
    <attendee_initials>A.1.</attendee_initials>
  </row>
</attendee>

Any clue where I am going wrong?


You are appending the text node to the row:

    $nodename = $attendee->createElement($tag);
    $nodename = $row->appendChild($nodename);

    $nodevalue = $attendee->createTextNode($value);
    $nodevalue = $row->appendChild($nodevalue);

You want to append it to the element you just created.

    $element = $attendee->createElement($tag);
    $textNode = $attendee->createTextNode($value);
    $element->appendChild($nodevalue);
    $row->appendChild($element);
0

精彩评论

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

关注公众号