John Davidson

javascript - Can't get xml from php

0 comments
Message:


I've been trying to get data from my database into a table on my website using js/php but I can't get the php to give a valid xml to the script.
This is the php code:


    <?php
$serverName = "localhost";
$userName = "***";
$password = "***";
$DBName = "DataBase1";

// Connect to DB
$connection = mysqli_connect($serverName, $userName, $password, $DBName);
if (!$connection)
{
die("Connection failed: " . mysqli_connect_error());
} else {
//echo "Connected to DB<br>";
}

// Collect data
$selectionSD = $connection->query("SELECT * FROM `SensorData`");
$selectionSL = $connection->query("SELECT * FROM `SensorLog`");

// Generating XML file
//$xmlSD = new XMLWriter();
//$xmlSD->openUri("php://output");
//$xmlSD->startDocument();
//$xmlSD->setIndent(true);
//$xmlSD->startElement("SensorDataTable");
//while ($row = mysqli_fetch_assoc($selectionSD))
//{
// $xmlSD->startElement("SensorData");
// $xmlSD->startElement("ID");
// $xmlSD->writeRaw($row["ID"]);
// $xmlSD->endElement();
// $xmlSD->startElement("SID");
// $xmlSD->writeRaw($row["SID"]);
// $xmlSD->endElement();
// $xmlSD->startElement("Value");
// $xmlSD->writeRaw($row["Value"]);
// $xmlSD->endElement();
// $xmlSD->startElement("Comment");
// $xmlSD->writeRaw($row["Comment"]);
// $xmlSD->endElement();
// $xmlSD->startElement("DateTime");
// $xmlSD->writeRaw($row["DateTime"]);
// $xmlSD->endElement();
// $xmlSD->endElement();
//}
//$xmlSD->endElement();
//header("Content-type: text/xml");
//$xmlSD->flush();
//$xmlSD->endDocument();

// Test code
$document = new DOMDocument('1.0', 'utf-8');
$document->formatOutput = true;

$root = $document->createElement('SensorDataTable');
$root = $document->appendChild($root);

while ($row = mysqli_fetch_assoc($selectionSD))
{
$node = $document->createElement('SensorData');
$node = $root->appendChild($node);

// ID
$dataNode = $document->createElement('ID');
$dataNode = $node->appendChild($dataNode);
$data = $document->createTextNode($row["ID"]);
$data = $dataNode->appendChild($data);
// SID
$dataNode = $document->createElement('SID');
$dataNode = $node->appendChild($dataNode);
$data = $document->createTextNode($row["SID"]);
$data = $dataNode->appendChild($data);
// Value
$dataNode = $document->createElement('Value');
$dataNode = $node->appendChild($dataNode);
$data = $document->createTextNode($row["Value"]);
$data = $dataNode->appendChild($data);
// Comment
$dataNode = $document->createElement('Comment');
$dataNode = $node->appendChild($dataNode);
$data = $document->createTextNode($row["Comment"]);
$data = $dataNode->appendChild($data);
// DateTime
$dataNode = $document->createElement('DateTime');
$dataNode = $node->appendChild($dataNode);
$data = $document->createTextNode($row["DateTime"]);
$data = $dataNode->appendChild($data);
}

echo $document->saveXML();

// Close DB connection
$connection->close();
?>

I've tried to generate the xml a different way but no luck (the commented lines was what I tried first).


To get the xml into a table I use js with this code:


console.log(xml);
let i;
const xmlDoc = xml.responseXML;
if (xmlDoc === null) {
console.log("Failed to get response.");
}
let table = "<tr><th>SID</th><th>Value</th></tr>";
const x = xmlDoc.getElementsByTagName("SensorData");
for (i = 0; i < x.length; i++) {
table += "<tr><td class='center'>" +
x[i].getElementsByTagName("SID")[0].childNodes[0].nodeValue +
"</td><td class='center'>" +
x[i].getElementsByTagName("Value")[0].childNodes[0].nodeValue +
"</td></tr>";
}
console.log("Changing DB display");
document.getElementById("DB").innerHTML = table;

Where xml = the path to the php file.


When trying to run this it gives an error that xmlDoc is null. If I run this using an actual xml file on my drive it works fine.


If I call the php file directly and inspect the source it shows this:


<?xml version="1.0" encoding="utf-8"?>
<SensorDataTable>
<SensorData>
<ID>1</ID>
<SID>1</SID>
<Value>19.50</Value>
<Comment>DHT_Temp</Comment>
<DateTime>2021-10-04 12:57:51</DateTime>
</SensorData>
<SensorData>
<ID>2</ID>
<SID>2</SID>
<Value>57.50</Value>
<Comment>DHT_Humi</Comment>
<DateTime>2021-10-04 12:57:55</DateTime>
</SensorData>
<SensorData>
<ID>3</ID>
<SID>1</SID>
<Value>19.60</Value>
<Comment>DHT_Temp</Comment>
<DateTime>2021-10-04 12:58:29</DateTime>
</SensorData>
<SensorData>
<ID>4</ID>
<SID>2</SID>
<Value>57.20</Value>
<Comment>DHT_Humi</Comment>
<DateTime>2021-10-04 12:58:32</DateTime>
</SensorData>
</SensorDataTable>

How can I fix this issue, I’ve searched the web but the solutions I've come across did not work (creating the xml in a different way for example).


Fixed: change in the php code, added header("Content-type: text/xml"); back:


<?php
$serverName = "localhost";
$userName = "***";
$password = "***";
$DBName = "LIDAc";

// Connect to DB
$connection = mysqli_connect($serverName, $userName, $password, $DBName);
if (!$connection)
{
die("Connection failed: " . mysqli_connect_error());
}

// Collect data
$selectionSD = $connection->query("SELECT * FROM `SensorData`");
$selectionSL = $connection->query("SELECT * FROM `SensorLog`");

// Generate XML file
$document = new DOMDocument('1.0', 'utf-8');
$document->formatOutput = true;

$root = $document->createElement('SensorDataTable');
$root = $document->appendChild($root);

while ($row = mysqli_fetch_assoc($selectionSD))
{
$node = $document->createElement('SensorData');
$node = $root->appendChild($node);

// ID
$dataNode = $document->createElement('ID');
$dataNode = $node->appendChild($dataNode);
$data = $document->createTextNode($row["ID"]);
$data = $dataNode->appendChild($data);
// SID
$dataNode = $document->createElement('SID');
$dataNode = $node->appendChild($dataNode);
$data = $document->createTextNode($row["SID"]);
$data = $dataNode->appendChild($data);
// Value
$dataNode = $document->createElement('Value');
$dataNode = $node->appendChild($dataNode);
$data = $document->createTextNode($row["Value"]);
$data = $dataNode->appendChild($data);
// Comment
$dataNode = $document->createElement('Comment');
$dataNode = $node->appendChild($dataNode);
$data = $document->createTextNode($row["Comment"]);
$data = $dataNode->appendChild($data);
// DateTime
$dataNode = $document->createElement('DateTime');
$dataNode = $node->appendChild($dataNode);
$data = $document->createTextNode($row["DateTime"]);
$data = $dataNode->appendChild($data);
}

header("Content-type: text/xml");

echo $document->saveXML();

// Close DB connection
$connection->close();
?>


My Answer:
you please provide more context or clarify your question?

Rate this post

3 of 5 based on 2741 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us