Laravel和DOMDocument createDocumentFragment

时间:2014-05-24 17:00:43

标签: php xml laravel xml-spreadsheet

我在Laravel控制器中有以下代码:

public function toXMLSpreadsheet()
{
    $doc = new DOMDocument();
    $doc->load('xmlspreadsheet.xml');
    $table = $doc->getElementsByTagName('Table')->item(0);

    $applications = Application::all();
    foreach($applications as $application)
    {
        $f = $doc->createDocumentFragment();
        $f->appendXML("
            <Row>
                <Cell><Data ss:Type=\"Number\">{$application->id}</Data></Cell>
                <Cell><Data ss:Type=\"String\">{$application->nume}</Data></Cell>
                <Cell><Data ss:Type=\"String\">{$application->prenume}</Data></Cell>
                <Cell><Data ss:Type=\"String\">{$application->cnp}</Data></Cell>
                <Cell><Data ss:Type=\"String\">{$application->adresa}</Data></Cell>
                <Cell><Data ss:Type=\"String\">{$application->localitate}</Data></Cell>
                <Cell><Data ss:Type=\"String\">{$application->judet}</Data></Cell>
                <Cell><Data ss:Type=\"String\">{$application->telefon}</Data></Cell>
                <Cell><Data ss:Type=\"String\">{$application->email}</Data></Cell>
                <Cell><Data ss:Type=\"String\">{$application->nationalitate}</Data></Cell>
            </Row>
        ");
        $table->appendChild($f);
    }

    $doc->save('Inscrisi.xml');

}

以及以下XML文件:

<?xml version="1.0" encoding="UTF-8"?>

<?mso-application progid="Excel.Sheet"?>

<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
  xmlns:o="urn:schemas-microsoft-com:office:office"
  xmlns:x="urn:schemas-microsoft-com:office:excel"
  xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
  xmlns:html="http://www.w3.org/TR/REC-html40">

  <Worksheet ss:Name="Table1">
    <Table>
    </Table>
  </Worksheet>
</Workbook>

当我在控制器中运行该函数时,我收到如下错误:

  

production.ERROR:异常'ErrorException',消息'DOMDocumentFragment :: appendXML():namespace error:在E:\ www \ practica \ app \ controllers \ ApplicationsController.php中未定义Type on Data的命名空间前缀ss :105

知道为什么吗?名称空间ss在XML文件中定义。

谢谢!

1 个答案:

答案 0 :(得分:0)

您需要在XML片段中为'ss'前缀定义名称空间。将具有正确名称空间的xmlns:ss添加到顶部元素(行)。