Laravel and DOMDocument createDocumentFragment


Laravel and DOMDocument createDocumentFragment

我在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>

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

生产。错误:异常'ErrorException'与消息'DOMDocumentFragment::appendXML():命名空间错误:命名空间前缀ss类型上的数据是没有定义'在E:'www'practica'app'controllers'ApplicationsController.php:105

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

谢谢!

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

相关文章:
  • 没有找到相关文章