douse8732 2015-01-11 22:44
浏览 15

从php页面创建XML文档[重复]

This question already has an answer here:

I have the following code which is displaying an xml page as with the URL 'ourcustomersdb.php'. I need this same information to be displayed with the URL ending in 'ourcustomers.xml'. (Hope that makes sense)

The code I have works, but I need the last line of code to create the xml document:

<?php
header ("Content-Type:text/xml");//Tell browser to expect xml
include ("config/init.php");
$query = "SELECT * FROM ourcustomers"; 
$result = mysqli_query($mysqli_conn, $query) or die ("Error in query: $query. ".mysql_error()); 
//Top of xml file
$_xml = '<?xml version="1.0"?>'; 
$_xml .="<ourcustomers>"; 
while($row = mysqli_fetch_array($result)) { 
$_xml .="<ourcustomer>"; 
$_xml .="<id>".$row['id']."</id>"; 
$_xml .="<customer>".$row['customer']."</customer>"; 
$_xml .="<work_done>".$row['work_done']."</work_done>"; 
$_xml .="</ourcustomer>"; 
} 
$_xml .="</ourcustomers>"; 
//Parse and create an xml object using the string
$xmlobj=new SimpleXMLElement($_xml);
//output
//print $xmlobj->asXML();
//write to a file
$xmlobj->asXML(ourcustomers.xml);
?>

The line that doesn't seem to be working is the $xmlobj->asXML(ourcustomers.xml). I need this to take the information gathered from the database to create an XML file. If I leave in the last print comment it displays the XML but on the page ourcustomersdb.php, but I need this to be displayed on ourcustomers.xml (hope this is making sense!). Can someone explain why as this is the first time I'm trying to do this so I'm not fully understanding it.

Any help would be great!

</div>
  • 写回答

1条回答 默认 最新

  • doujia7517 2015-01-11 22:47
    关注

    You need to wrap ourcustomers.xml in quotes, so that it is a string. Otherwise you will get a syntax error.

    $xmlobj->asXML('ourcustomers.xml');
    

    Also, you don't have to use SimpleXMLElement to write an XML file, you could just use file_put_contents.

    file_put_contents('ourcustomers.xml', $_xml);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法