2011-06-12 11 views
0

DOMDocument를 사용하여 mysql 데이터베이스에서 데이터를 가져온 다음 나중에 읽을 수있는 구조화 된 XML에 넣는 작은 스크립트가 있습니다.PHP로 데이터를 XML로 출력

내 XML의 올바른 구조를 만들기 위해 PHP 코드를 조정하는 데 약간의 문제가 있습니다.

코드 :

<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 코드는 다음과 같습니다 :

(210)

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(); 

?> 

나는이 XML 파일에서 읽을 수해야하는지에 대한 포인터로 나중에 사용됩니다 하나 개의 고유 한 태그 (장치)를 가지고 코드가 필요합니다. DOMDocument는 내가 가장 잘 알고있는 함수이기 때문에 사용하고 있습니다.

모든 아이디어는 높이 평가 될 것입니다.

미리 감사드립니다.

답변

0

노드 배열을 만들어 매번 노드를 생성하는 대신 노드가 존재하는지 확인해야합니다. 이렇게하려면 :

[...] 
$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에 추가하고 속성을 설정합니다.
  • 이 시점에서 우리는 노드가 만들어 졌음을 알고 있으므로 간단히 노드에 새 요소를 추가합니다.
+0

입력 해 주셔서 감사합니다. 귀하의 코드를 시도했지만 예기치 않은 오류가 발생합니다 : 구문 분석 오류 : 예기치 않은 '{'in D : \ wamp \ www \ fiberglass \ exportintoxml2.php on line 41 "if"문 시작의 "{"입니다. 나는 왜 그런지 이해할 수 없다. –

+0

왜냐하면 if는 괄호가 없기 때문입니다. 내가 지금 고칠거야. – Lumbendil

+0

감사합니다. 그 많은 도움이 :) 지금은 자바 스크립트를 다루어야 ...:) –

0

문제는 각 iteraion에 대해 새로운 DEVS 요소를 만드는 것입니다.

다른 방법으로는 다른 쿼리를 사용하여 DEVS 그룹을 만드는 것입니다. 코드의 일부는 다음과 같이 어떻게 든 looke됩니다

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

그럼 당신은 그것을 추가 할 노드, 단지 확인 배열로 메인 루프에 장착이 만든 노드에 아이들을 할당 할 수 있습니다.

+1

이것은 데이터가 현재 쿼리로 이미 페치되었으므로 필요하지 않아야하는 추가 쿼리를 추가하는 것을 의미합니다. – Lumbendil

관련 문제