在插入值之前关闭的XML标记

时间:2011-08-31 09:43:14

标签: php

以下代码:

// 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($value);
        $nodevalue = $row->appendChild($nodevalue);
    }
}

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

产地:

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

而不是:

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

我出错的任何线索?

1 个答案:

答案 0 :(得分:1)

您要将文本节点附加到行:

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

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

您想将它附加到刚创建的元素。

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