生成 XML 并将其保存到磁盘问题

发布于 2024-12-20 17:28:53 字数 2034 浏览 0 评论 0原文

当我生成 XML 文件并将其保存到磁盘时,收到以下错误消息。我收到“XML 文档必须具有顶级元素”。可能是什么问题?我很感激任何帮助。如果我使用

echo sqlToXml($result, "SalesOrder", "sales");

xml数据在IE中显示。但由于某种原因,当我尝试保存它时,它会抛出错误。

     header("content-type:text/xml");
     $connection = mysql_connect( $db_host, $db_username, $db_password );
    if (!$connection){
    die ("Could not connect to the database: <br />". mysql_error());
    }
    // Select the database
    $db_select=mysql_select_db($db_database);
    if (!$db_select){
    die ("Could not select the database: <br />". mysql_error());
    }

$result = mysql_query("SELECT * from table1");

header("Content-Type: application/xml");
$xmlDocument1 = sqlToXml($result, "SalesOrder", "sales");
$filename= 'c:\\xmldoc\\results.xml';

file_put_contents($filename ,$xmlDocument1);

function sqlToXml($queryResult, $rootElementName, $childElementName)
{

$xmlData = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n"; 
$xmlData .= "<" . $rootElementName . ">";

while($record = mysql_fetch_object($queryResult))
{ 
    $xmlData .= "<" . $childElementName . ">";
    for ($i = 0; $i < mysql_num_fields($queryResult); $i++)
    { 
        $fieldName = mysql_field_name($queryResult, $i); 
        $xmlData .= "<" . $fieldName . ">";
        if(!empty($record->$fieldName))
            $xmlData .= $record->$fieldName; 
        else
            $xmlData .= "null"; 

        $xmlData .= "</" . $fieldName . ">"; 
    } 
    $xmlData .= "</" . $childElementName . ">"; 
} 
$xmlData .= "</" . $rootElementName . ">"; 
}

这是结果 XML。

 <?xml version="1.0" encoding="ISO-8859-1" ?>
  <SalesOrder>
    <Sales>
     <SalesOrderNumber>SO43</SalesOrderNumber>
     <PurchaseOrderNumber>PO18</PurchaseOrderNumber>
     <AccountNumber>3333</AccountNumber>
     <OrderDate>2001-07-01</OrderDate>
   </Sales>
  </SalesOrder>

I am getting the following error message when I generate the XML file and save it to the disk. I am getting "XML document must have a top level element". What could be the problem? I appreciate any help. If I use

echo sqlToXml($result, "SalesOrder", "sales");

the xml data shows in IE. But for some reason it throws an error when I try to save it.

     header("content-type:text/xml");
     $connection = mysql_connect( $db_host, $db_username, $db_password );
    if (!$connection){
    die ("Could not connect to the database: <br />". mysql_error());
    }
    // Select the database
    $db_select=mysql_select_db($db_database);
    if (!$db_select){
    die ("Could not select the database: <br />". mysql_error());
    }

$result = mysql_query("SELECT * from table1");

header("Content-Type: application/xml");
$xmlDocument1 = sqlToXml($result, "SalesOrder", "sales");
$filename= 'c:\\xmldoc\\results.xml';

file_put_contents($filename ,$xmlDocument1);

function sqlToXml($queryResult, $rootElementName, $childElementName)
{

$xmlData = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n"; 
$xmlData .= "<" . $rootElementName . ">";

while($record = mysql_fetch_object($queryResult))
{ 
    $xmlData .= "<" . $childElementName . ">";
    for ($i = 0; $i < mysql_num_fields($queryResult); $i++)
    { 
        $fieldName = mysql_field_name($queryResult, $i); 
        $xmlData .= "<" . $fieldName . ">";
        if(!empty($record->$fieldName))
            $xmlData .= $record->$fieldName; 
        else
            $xmlData .= "null"; 

        $xmlData .= "</" . $fieldName . ">"; 
    } 
    $xmlData .= "</" . $childElementName . ">"; 
} 
$xmlData .= "</" . $rootElementName . ">"; 
}

Here is the result XML.

 <?xml version="1.0" encoding="ISO-8859-1" ?>
  <SalesOrder>
    <Sales>
     <SalesOrderNumber>SO43</SalesOrderNumber>
     <PurchaseOrderNumber>PO18</PurchaseOrderNumber>
     <AccountNumber>3333</AccountNumber>
     <OrderDate>2001-07-01</OrderDate>
   </Sales>
  </SalesOrder>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤蝉 2024-12-27 17:28:53

您是否错过了 sqlToXml-Function 中的 return 语句?

    } 
    $xmlData .= "</" . $childElementName . ">"; 
  } 
  $xmlData .= "</" . $rootElementName . ">"; 
  return $xmlData;  
}

Did you miss the return statement in your sqlToXml-Function?

    } 
    $xmlData .= "</" . $childElementName . ">"; 
  } 
  $xmlData .= "</" . $rootElementName . ">"; 
  return $xmlData;  
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文