我创建了一个Web应用程序,用户在其中输入数据,然后使用ms sql server数据库中的值生成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中的人
答案 0 :(得分:0)
您无法向单个http请求输出两个回复。
就像图像或下载一样,您需要一个单独的请求,只输出XML并为用户提供一种调用方式(链接,表单,重定向,Javascript)。