使用 PHP 将数据输出为 XML
我这里有一个小脚本,它使用 DOMDocument 从 mysql 数据库获取数据并将其放入结构化 XML 中,稍后用于读取。
我在调整 php 代码以创建正确的 XML 结构时遇到了一些麻烦。
现在我的代码看起来像这样:
代码:
<markers>
<DEVS DEVICE="DEV10">
<marker USER="PRIVET_!" DATA1="0578" DATA2="0A57" TIME="18:16:40" />
</DEVS>
<DEVS DEVICE="DEV10">
<marker USER="PRIVET_!" DATA1="0578" DATA2="0A55" TIME="18:16:05" />
</DEVS>
<DEVS DEVICE="DEV5">
<marker USER="PRIVET_!" DATA1="0578" DATA2="0A55" TIME="18:16:05" />
</DEVS>
<DEVS DEVICE="DEV5">
<marker USER="PRIVET_!" DATA1="0578" DATA2="0A55" TIME="18:16:05" />
</DEVS>
</markers>
我希望我的代码看起来像这样:
代码:
<markers>
<DEVS DEVICE="DEV10">
<marker USER="PRIVET_!" DATA1="0578" DATA2="0A57" TIME="18:16:40" />
<marker USER="PRIVET_!" DATA1="0578" DATA2="0A55" TIME="18:16:05" />
</DEVS>
<DEVS DEVICE="DEV5">
<marker USER="PRIVET_!" DATA1="0578" DATA2="0A55" TIME="18:16:05" />
<marker USER="PRIVET_!" DATA1="0578" DATA2="0A55" TIME="18:16:05" />
</DEVS>
</markers>
这是我现在拥有的 PHP 代码:
PHP 代码:
<?php
require("config.php");
// Start XML file, create parent node
$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);
// Opens a connection to a MySQL server
$connection=mysql_connect ($server, $db_user, $db_pass);
if (!$connection) { die('Not connected : ' . mysql_error());}
// Set the active MySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}
// Select all the rows in the markers table
$query = "SELECT * FROM input WHERE (DEVS = 'DEV5' or DEVS = 'DEV10') ORDER BY TIME DESC";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
header("Content-type: text/xml");
// Iterate through the rows, adding XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
$node1 = $dom->createElement("DEVS");
$parnode->appendChild($node1);
$marker = $dom->createElement("marker");
$node1->appendChild($marker);
$marker->setAttribute("USER", $row['USER']);
$marker->setAttribute("DATA1", $row['DATA1']);
$marker->setAttribute("DATA2", $row['DATA2']);
$marker->setAttribute("TIME", $row['TIME']);
$node1->setAttribute("DEVICE", $row['DEVS']);
}
echo $dom->saveXML();
?>
我需要代码有一个唯一的标签(DEVICE ) 稍后将用作指向应从此 XML 文件中读取内容的指针。 我正在使用 DOMDocument,因为这是我最熟悉的功能。
任何想法将不胜感激。
提前致谢!
I have a little script here which uses DOMDocument to get my data from my mysql database and put it in a structured XML which is later on used to be read from.
I am having a little trouble adjusting my php code to create the right structure of my XML.
Right now my code looks like this:
Code:
<markers>
<DEVS DEVICE="DEV10">
<marker USER="PRIVET_!" DATA1="0578" DATA2="0A57" TIME="18:16:40" />
</DEVS>
<DEVS DEVICE="DEV10">
<marker USER="PRIVET_!" DATA1="0578" DATA2="0A55" TIME="18:16:05" />
</DEVS>
<DEVS DEVICE="DEV5">
<marker USER="PRIVET_!" DATA1="0578" DATA2="0A55" TIME="18:16:05" />
</DEVS>
<DEVS DEVICE="DEV5">
<marker USER="PRIVET_!" DATA1="0578" DATA2="0A55" TIME="18:16:05" />
</DEVS>
</markers>
And I would like to have my code look like this:
Code:
<markers>
<DEVS DEVICE="DEV10">
<marker USER="PRIVET_!" DATA1="0578" DATA2="0A57" TIME="18:16:40" />
<marker USER="PRIVET_!" DATA1="0578" DATA2="0A55" TIME="18:16:05" />
</DEVS>
<DEVS DEVICE="DEV5">
<marker USER="PRIVET_!" DATA1="0578" DATA2="0A55" TIME="18:16:05" />
<marker USER="PRIVET_!" DATA1="0578" DATA2="0A55" TIME="18:16:05" />
</DEVS>
</markers>
And here is the PHP code I have at the moment:
PHP Code:
<?php
require("config.php");
// Start XML file, create parent node
$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);
// Opens a connection to a MySQL server
$connection=mysql_connect ($server, $db_user, $db_pass);
if (!$connection) { die('Not connected : ' . mysql_error());}
// Set the active MySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}
// Select all the rows in the markers table
$query = "SELECT * FROM input WHERE (DEVS = 'DEV5' or DEVS = 'DEV10') ORDER BY TIME DESC";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
header("Content-type: text/xml");
// Iterate through the rows, adding XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
$node1 = $dom->createElement("DEVS");
$parnode->appendChild($node1);
$marker = $dom->createElement("marker");
$node1->appendChild($marker);
$marker->setAttribute("USER", $row['USER']);
$marker->setAttribute("DATA1", $row['DATA1']);
$marker->setAttribute("DATA2", $row['DATA2']);
$marker->setAttribute("TIME", $row['TIME']);
$node1->setAttribute("DEVICE", $row['DEVS']);
}
echo $dom->saveXML();
?>
I need the code to have one unique tag (DEVICE) which will be used later on as a pointer to what should be read from this XML file.
I am using DOMDocument since this is the function I am most familiar with.
Any ideas would be highly appreciated.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该创建一个节点数组,并检查该节点是否存在,而不是每次都创建它。为此:
这样,我们要做的是:
You should make an array of nodes, and check if the node exists instead of creating it each time. To do so:
This way, what we're doing is:
您的问题是,对于每个迭代,您都会创建新的 DEVS 元素。
方法之一是使用另一个查询来创建 DEVS 组。部分代码看起来像这样:
然后,您可以将子节点分配给加载到数组中的这些创建的节点,并在主循环中检查要附加它的节点。
Your problem is that for each iteraion you create new DEVS element.
One of ways is to use another query to create DEVS groups. Part of the code will looke somehow like this:
Then you can assign children to these created nodes loaded into an array and in the main loop just check, which node to append it.