我在将 XML 文件加载到网页并显示其中包含的信息时遇到了一个小问题。对于这个程序,我试图用加载到 XML 文件的恒定数据流更新一个非常简单的网页。当我的程序发现新数据时(通常每隔几秒一次),它会修改 XML 文件并保存它。另一个 Web 客户端(在 Netbeans 中)运行显示此信息的网页。截至目前,我的问题是当我刷新页面时,没有新数据出现(相同的值仍然存在,即使我知道它们不应该出现)。然后我检查 XML 文件,发现新数据已成功写入。奇怪的是,一旦我打开 xml 文件查看信息,一旦我刷新页面,网页就会自行更新。这似乎是更新网页的唯一方法是我自己打开 xml 文件。我的问题是有没有人知道发生了什么,你能提供解决这个问题的方法吗?
我已经提供了我的ModifyXML.java代码和index.jsp页面以供引用
修改 XML
import java.io... //all import statements not shown
public class ModifyXML {
//provide the byte array and the number of the sensor that needs to be
//updated in the XML file
public ModifyXML(byte[] array, int signal) {
try {
//create a new Document Builder to modify the XML file
String filepath = "C:\\user\\projects\\onlineClient\\web\\HTTP.xml";
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(filepath);
// Get the root element
Node Sensor = doc.getFirstChild();
// Get the staff elements by tag name directly
Node data = doc.getElementsByTagName("Data").item(0);
Node data2 = doc.getElementsByTagName("Data2").item(0);
/* Methods to update the information are not shown as they work fine (it saves space)*/
// write the content into xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(filepath));
transformer.transform(source, result);
System.out.println("Done");
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (TransformerException tfe) {
tfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (SAXException sae) {
sae.printStackTrace();
}
}
}
index.jsp
<html>
<title>Home Monitor Web Client</title>
<body>
<h1>Home Monitor</h1>
<p id="demo">Paragraph.</p>
<p id="demo2">Paragraph.</p>
<br>
<p id="demo3">Paragraph.</p>
<p id="demo4">Paragraph.</p>
<script type="text/javascript">
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","HTTP.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
//document.write("<table border='1'>");
var data=xmlDoc.getElementsByTagName("data");
var data2=xmlDoc.getElementsByTagName("data2");
for (i=0;i<temperature.length;i++)
{
document.getElementById("demo").innerHTML="ID Number: "+data[i].getElementsByTagName("ID")[0].childNodes[0].nodeValue;
document.getElementById("demo2").innerHTML="Reading: "+data[i].getElementsByTagName("Value")[0].childNodes[0].nodeValue;
document.getElementById("demo3").innerHTML="ID Number: "+data2[i].getElementsByTagName("ID")[0].childNodes[0].nodeValue;
document.getElementById("demo4").innerHTML="Reading: "+data2[i].getElementsByTagName("Value")[0].childNodes[0].nodeValue;
}
</script>
</body>
</html>
最佳答案
您可以将 html5 事件源与服务器端事件一起使用。在
上有一篇很好的文章他们展示了这个示例(用 PHP 和 js 编写,但您应该能够将其转换为 java)
服务器代码 (PHP)
<?php
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache'); // recommended to prevent caching of event data.
/**
* Constructs the SSE data format and flushes that data to the client.
*
* @param string $id Timestamp/id of this connection.
* @param string $msg Line of text that should be transmitted.
*/
function sendMsg($id, $msg) {
echo "id: $id" . PHP_EOL;
echo "data: $msg" . PHP_EOL;
echo PHP_EOL;
ob_flush();
flush();
}
$serverTime = time();
sendMsg($serverTime, 'server time: ' . date("h:i:s", time()));
客户端代码(js)
if (!!window.EventSource) {
var source = new EventSource('stream.php');
source.onmessage = function(e) {
document.body.innerHTML += e.data + '<br>';
};
} else {
// Result to xhr polling :(
}
在这里查看教程。 http://www.html5rocks.com/en/tutorials/eventsource/basics/
关于java - 使用 XML 文件持续更新网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11631797/