You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

从SQL数据库生成XML文件时持续报文档为空错误,求排查

Fixing the "Document is empty" XML Export Error

Let's walk through the issues in your code that are causing this error and invalid XML output:

1. Broken parseToXML Function

Your string replacement logic has two critical flaws:

  • Every replacement uses the original $htmlStr instead of building on the modified $xmlStr, so only the last replacement (& → &) actually takes effect.
  • The single-quote replacement line has a syntax error: ''$htmlStr is invalid PHP syntax, which will throw an error and halt your script entirely (leading to the empty document message).

Fixed function:

function parseToXML($htmlStr) { 
    $xmlStr = $htmlStr;
    $xmlStr = str_replace('<','&lt;',$xmlStr); 
    $xmlStr = str_replace('>','&gt;',$xmlStr); 
    $xmlStr = str_replace('"','&quot;',$xmlStr); 
    $xmlStr = str_replace("'",'&#39;',$xmlStr); 
    $xmlStr = str_replace("&",'&amp;',$xmlStr); 
    return $xmlStr; 
}

2. Invalid Output Breaking XML Structure

You're outputting non-XML content before and within your XML document:

  • echo "test1 \n\n"; and other test outputs come before the XML declaration <?xml version='1.0' ?>, which violates XML rules (the declaration must be the first line of the document).
  • The <br> tags and raw row content in your first loop are plain HTML/text that doesn't belong in an XML file—this will make the document invalid even if it loads.

Fix: Remove all debug echo statements (test1/test2/test3) and the first loop that outputs <br> tags.

3. Result Set Exhausted Before XML Nodes Are Generated

Your first while($row = mysqli_fetch_assoc($result)) loop reads all rows from the result set, so when you try to loop again later to generate <marker> nodes, there are no rows left to fetch.

Fix: Delete the first loop entirely (it's just debug output anyway) so the result set is intact for generating your XML nodes.

4. Potential Header Issues from config.php

Even if you placed the header() call at the top, if config.php outputs any content (like whitespace, echo statements, or a UTF-8 BOM), PHP will send headers automatically before your header() call, which can break the XML content type and cause silent failures.

Fix: Check config.php to ensure it has no output (no leading/trailing spaces, no echo statements) and is saved as UTF-8 without BOM.


Full Corrected Code

<?php 
header('Content-type: text/xml'); 
require("config.php"); 

function parseToXML($htmlStr) { 
    $xmlStr = $htmlStr;
    $xmlStr = str_replace('<','&lt;',$xmlStr); 
    $xmlStr = str_replace('>','&gt;',$xmlStr); 
    $xmlStr = str_replace('"','&quot;',$xmlStr); 
    $xmlStr = str_replace("'",'&#39;',$xmlStr); 
    $xmlStr = str_replace("&",'&amp;',$xmlStr); 
    return $xmlStr; 
} 

$sql = "SELECT * FROM markers;"; 
$result = mysqli_query($connection, $sql); 

// Start XML document
echo "<?xml version='1.0' ?>"; 
echo '<markers>'; 

if(mysqli_num_rows($result) > 0) { 
    while($row = mysqli_fetch_assoc($result)){ 
        echo '<marker '; 
        echo 'id="' . $row['id'] . '" '; 
        echo 'name="' . parseToXML($row['name']) . '" '; 
        echo 'address="' . parseToXML($row['address']) . '" '; 
        echo 'lat="' . parseToXML($row['lat']) . '" '; 
        echo 'lng="' . parseToXML($row['lng']) . '" '; 
        echo 'type="' . parseToXML($row['type']) . '" '; 
        echo '/>'; 
    } 
} 

echo '</markers>'; 
?>

内容的提问来源于stack exchange,提问作者Ryan Brown

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:54:17