PHP DOMDocument output HTML page code



I've made a web application in which the user inputs data and then an xml is generated with values from an ms sql server database, since users are going to be using this locally being hosted on a server, I need to trigger downloads instead of saving to a location, and while it does that, the output also includes the HTML in the page, this is the code



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");
}


I've searched all over Google but I can only find people who actually WANT to output HTML code into an xml


No comments:

Post a Comment