outputting data with PHP into XML
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",开发者_如何学JAVA $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!
You should make an array of nodes, and check if the node exists instead of creating it each time. To do so:
[...]
$nodes = array();
while ($row = @mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
$node_key = $row['DEVS'];
if( !array_key_exists( $node_key, $nodes ) )
{
$nodes[$node_key] = $dom->createElement("DEVS");
$parnode->appendChild($nodes[$node_key]);
$nodes[$node_key]->setAttribute("DEVICE", $row['DEVS']);
}
$marker = $dom->createElement("marker");
$nodes[$node_key]->appendChild($marker);
$marker->setAttribute("USER", $row['USER']);
$marker->setAttribute("DATA1", $row['DATA1']);
$marker->setAttribute("DATA2", $row['DATA2']);
$marker->setAttribute("TIME", $row['TIME']);
}
This way, what we're doing is:
- Get the node key.
- If the node for that key isn't created, we create it, adding it to the DOM and setting it's attribute.
- At this point, we know for sure that the node is created, so we simply append the new element to the node.
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:
$query = mysql_query("SELECT devs FROM input WHERE (devs = 'DEV5' or devs = 'DEV10') GROUP BY devs");
$result = mysql_query($query);
$devsNodes = array();
while ($row = mysql_fetch_array($result)){
$node = $dom->createElement("DEVS");
$node->setAttribute("DEVICE", $row['DEVS']);
$devsNodes[] = $node;
}
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.
精彩评论