使用 PHP 将数据输出为 XML

发布于 2024-11-15 11:44:12 字数 2746 浏览 0 评论 0原文

我这里有一个小脚本,它使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

没有伤那来痛 2024-11-22 11:44:12

您应该创建一个节点数组,并检查该节点是否存在,而不是每次都创建它。为此:

[...]
$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']);
}

这样,我们要做的是:

  • 获取节点密钥。
  • 如果未创建该键的节点,我们将创建它,将其添加到 DOM 并设置其属性。
  • 此时,我们确定节点已创建,因此我们只需将新元素附加到该节点即可。

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.
伤痕我心 2024-11-22 11:44:12

您的问题是,对于每个迭代,您都会创建新的 DEVS 元素。

方法之一是使用另一个查询来创建 DEVS 组。部分代码看起来像这样:

$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;
}

然后,您可以将子节点分配给加载到数组中的这些创建的节点,并在主循环中检查要附加它的节点。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文