Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-12-2011, 07:30 PM   PM User | #1
spy_eye
New to the CF scene

 
Join Date: Jun 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
spy_eye is an unknown quantity at this point
outputting data in XML with PHP

Hello everyone!

First of all, I am new to this forum and I hope I am writing in the right section. If not, I would kindly ask a moderator to move my thread to the place it belongs to. Thanks!

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!
spy_eye is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:13 PM.


Advertisement
Log in to turn off these ads.