日期:2014-05-17  浏览次数:20422 次

php生成xml字符串
我现在有个检索后的结果集
USER_ID = 1
USER_NAME= 小张
POINT = 80

USER_ID = 2
USER_NAME=小李
POINT = 60

USER_ID = 3
USER_NAME= 小王
POINT = 70

想生成一个xml的字符串如下
"<?xml version='1.0' encoding='UTF-8'?>
<ExportData>
<Body>
<Content>
<Id>1</Id><Name小张</Name><Point>80</Point>
</Content>
<Content>
<Id>2</Id><Name>小李</Name><Point>60</Point>
</Content>
<Content>
<Id>3</Id><Name>小王</Name><Point>70</Point>
</Content>
</Body>
</ExportData>";
请教各位大侠怎么实现啊











------解决方案--------------------
PHP code
$xml = <<<xml
 <?xml version='1.0' encoding='UTF-8'?>
 <ExportData>
 <Body>
xml;
while($row = mysql_fetch_assoc($queryResult) )
{
    $xml .=<<<xml
   <Content>
      <Id>{$row['USER_ID']}</Id>
      <Name>{$row['USER_NAME']}</Name>
      <Point>{$row['POINT']}</Point>
   </Content>
xml;
}
$xml .=<<<xml
  </Body>
  </ExportData>
xml;

------解决方案--------------------
右键源代码。。。
想要标准的输出xml 在echo之前加上 header('Content-Type:text/xml');
------解决方案--------------------
那是因为浏览器把这个XML字符串当html文档解析了,你可以通过查看源代码看到完整的结果。
在代码的最前面加一句 header('Content-type: text/xml; charset=UTF-8'); ,
并且把 
$xml = <<<xml
<?xml version='1.0' encoding='UTF-8'?>
中的 <?xml 挪到最前面去,前面不能有任何输出。

一切ok