【问题标题】:PHP DOMDocument output HTML page codePHP DOMDocument 输出 HTML 页面代码
【发布时间】:2015-01-09 11:56:26
【问题描述】:

我制作了一个 Web 应用程序,用户在其中输入数据,然后使用来自 ms sql 服务器数据库的值生成一个 xml,因为用户将在本地使用托管在服务器上的这个,我需要触发下载而不是保存到一个位置,虽然这样做,输出还包括页面中的 HTML,这是代码

if(isset($_POST['Enviar']))
{
    $dia = $_POST['dia'];
    $mes = $_POST['mes'];
    $anio = $_POST['anio'];
    $fechao = $dia.'/'.$mes.'/'.$anio;
    $rifr = 'J'.$_POST['rifr'];
    $cod = $_POST['cod'];
    $xml = new DOMDocument('1.0', 'ISO-8859-1');
    $sql = "SELECT        REPLACE(REPLACE(REPLACE(dbo.SAPROV.ID3, '-', ''), '.', ''), ' ', '') AS RIF, RIGHT(REPLACE(REPLACE(REPLACE(SAACXP_1.NumeroD, '-', ''), 'a', ''), ' ', ''), 8) 
                             AS N_FACTURA, RIGHT(REPLACE(REPLACE(SAACXP_1.NroCtrol, '-', ''), ' ', ''), 8) AS NroCtrol, '0' + dbo.SAPAGCXP.CodRete AS CodOper, 
                             dbo.SAPAGCXP.BaseReten AS Monto, ISNULL(dbo.SAOPER.Clase, '2') AS Porcentaje, SUBSTRING(CONVERT(varchar, dbo.SAACXP.FechaE, 112), 1, 6) AS Mes
    FROM            dbo.SAOPER RIGHT OUTER JOIN
                             dbo.SAACXP AS SAACXP_1 RIGHT OUTER JOIN
                             dbo.SAPAGCXP RIGHT OUTER JOIN
                             dbo.SAPROV INNER JOIN
                             dbo.SAACXP ON dbo.SAPROV.CodProv = dbo.SAACXP.CodProv ON dbo.SAPAGCXP.NumeroD = dbo.SAACXP.NumeroD ON 
                             SAACXP_1.NroUnico = dbo.SAACXP.NroRegi ON dbo.SAOPER.CodOper = dbo.SAACXP.CodOper
    WHERE        (dbo.SAACXP.TipoCxP = '21')";
    $stmt = sqlsrv_query($conex, $sql) or die('Query failed!');
    while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC))
    {
        $root = $xml->createElement("RelacionRetencionesISLR");
        $xml->appendChild($root);
        $root->setAttribute("RifAgente", $row['RIF']);
        $root->appendChild($xml->createAttribute('Periodo'))->appendChild($xml->createTextNode($row['Mes'])); 
        //
        $a1 = $xml->createElement("RifRetenido");
        $rif = $xml->createTextNode($rifr);
        $a1->appendChild($rif);
        //
        $a2 = $xml->createElement("NumeroFactura");
        $nfactura = $xml->createTextNode($row['N_FACTURA']);
        $a2->appendChild($nfactura);
        //
        $a3 = $xml->createElement("NumeroControl");
        $ncontrol = $xml->createTextNode($row['NroCtrol']);
        $a3->appendChild($ncontrol);
        //
        $a4 = $xml->createElement("FechaOperacion");
        $fecha = $xml->createTextNode($fechao);
        $a4->appendChild($fecha);
        //
        $a5 = $xml->createElement("CodigoConcepto");
        $concepto = $xml->createTextNode($cod);
        $a5->appendChild($concepto);
        //
        $a6 = $xml->createElement("MontoOperacion");
        $monto = $xml->createTextNode($row['Monto']);
        $a6->appendChild($monto);
        //
        $a7 = $xml->createElement("PorcentajeRetencion");
        $porcentaje = $xml->createTextNode($row['Porcentaje']);
        $a7->appendChild($porcentaje);
        //
        $book = $xml->createElement("DetalleRetencion");
        $book->appendChild($a1);
        $book->appendChild($a2);
        $book->appendChild($a3);
        $book->appendChild($a4);
        $book->appendChild($a5);
        $book->appendChild($a6);
        $book->appendChild($a7);
        $root->appendChild($book);
    }
    $xml->formatOutput = true;
    $name = strftime('backup_%m_%d_%Y.xml');
    header('Content-Disposition: attachment;filename=' . $name);
    header('Content-Type: text/xml');
    $xml->save('php://output');
//  echo $xml->saveXML();
//  $xml->save("mybooks.xml") or die("Error");
}

我在 Google 上搜索过,但我只能找到真正想要将 HTML 代码输出到 xml 中的人

【问题讨论】:

    标签: php html xml domdocument


    【解决方案1】:

    您不能对单个 http 请求输出两个响应。

    与图像或下载非常相似,您需要一个单独的请求,该请求仅输出 XML 并为用户提供调用它的方式(链接、表单、重定向、Javascript)。

    【讨论】:

    • 我看不出这与我正在做或想要的任何事情有什么关系,你看,单击按钮后,会生成一个 XML,然后在您打开 xml 时提示下载文件你应该只看到生成的 xml 数据,但 xml 还包括我页面的 HTML( ...等...),此代码包含在我页面上的 .php 文件中。跨度>
    • 这是两个响应,XML 和 HTML。 $xml->save('php://output');echo 相同,header() 更改单个响应的标头。单击该按钮必须调用一个 PHP 脚本,该脚本只输出 XML,不输出其他任何内容。
    猜你喜欢
    • 2011-01-21
    • 1970-01-01
    • 2017-07-28
    • 1970-01-01
    • 1970-01-01
    • 2011-09-07
    • 1970-01-01
    相关资源
    最近更新 更多