我在php部分是begginer所以请帮帮我。这是我的php脚本,它有助于生成XML文件
<?php
mysql_connect('localhost', 'root', 'root');
mysql_select_db('dcu');
$sql = "SELECT meterId FROM meter";
$res = mysql_query($sql);
$xml = new XMLWriter();
$xml->openURI("php://output");
$xml->startDocument();
$xml->setIndent(true);
$xml->startElement('meteries');
while ($row = mysql_fetch_assoc($res)) {
$xml->startElement("meterId");
$xml->writeRaw($row['meterId']);
$xml->endElement();
}
$xml->endElement();
header('Content-type: text/xml');
$xml->flush();
?>
After running this php script i got this xml file in my terminal as well as in browser
<?xml version="1.0"?>
<cemeteries>
<meterId>1</meterId>
<meterId>2</meterId>
</cemeteries>
我的问题是如何将这个生成的xml保存在其他一些xml文件中
由于
答案 0 :(得分:1)
如果要将其保存在服务器上,可以这样做:
<?php
mysql_connect('localhost', 'root', 'root');
mysql_select_db('dcu');
$sql = "SELECT meterId FROM meter";
$res = mysql_query($sql);
$xml = new XMLWriter();
$xml->openMemory();
$xml->startDocument();
$xml->setIndent(true);
$xml->startElement('meteries');
while ($row = mysql_fetch_assoc($res))
{
$xml->startElement("meterId");
$xml->writeRaw($row['meterId']);
$xml->endElement();
}
$xml->endElement();
$content = $xml->flush();
$fp = fopen('data.xml', 'w');
fwrite($fp, $content);
fclose($fp);
header('Content-type: text/xml');
echo $content;
在fopen()模式下&#39; w&#39;如果文件不存在,PHP将尝试创建它。确保PHP有足够的权限来编写。