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는 내가 가장 잘 알고있는 함수이기 때문에 사용하고 있습니다.
모든 아이디어는 높이 평가 될 것입니다.
미리 감사드립니다.
입력 해 주셔서 감사합니다. 귀하의 코드를 시도했지만 예기치 않은 오류가 발생합니다 : 구문 분석 오류 : 예기치 않은 '{'in D : \ wamp \ www \ fiberglass \ exportintoxml2.php on line 41 "if"문 시작의 "{"입니다. 나는 왜 그런지 이해할 수 없다. –
왜냐하면 if는 괄호가 없기 때문입니다. 내가 지금 고칠거야. – Lumbendil
감사합니다. 그 많은 도움이 :) 지금은 자바 스크립트를 다루어야 ...:) –