Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6022179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:49:17+00:00 2026-05-23T03:49:17+00:00

I have a little script here which uses DOMDocument to get my data from

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T03:49:18+00:00Added an answer on May 23, 2026 at 3:49 am

    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.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a little python script that pulls emails from a POP mail address
I have wrote this little script which generates a nice random password. The problem
I have a problem with a little C script which should run as a
I am coding a little script to save some data from Internet every single
I have a jQuery slider which uses little thumbnails in the bottom left and
Just wondering what little scripts/programs people here have written that helps one with his
I have this little script to toggle a contact form when a button is
I have a little Perl script (On Windows) that checks some files for me
I'm a jQuery newbie, and I have trouble with a little script I created.
I have a little problem with a simple vbScript. The script has to run

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.