通过Arduino以太网套接字发送XML

时间:2016-11-08 16:51:02

标签: xml sockets arduino

我做了一个测量温度的简单软件。我不想将XML发送到外部浏览器或其他软件。当我用浏览器连接到Arduino时,我得到这张照片(如下)。我做错了什么?:

enter image description here

发送xml的代码是:

EthernetClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
  if (client.available()) {
    char c = client.read();
    Serial.write(c);

     if (c == '\n' && currentLineIsBlank) {
      client.println("HTTP/1.1 200 OK");
      client.println("Content-Type: text/xml;charset=UTF-8");
      client.println("Connection: close");  // the connection will be closed after completion of the response
      client.println();
      client.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
      client.println("<TEMP>");

      client.print(calcTemp(cnt1), 3);
      client.println("<TEMP/>");


      break;
    }
    if (c == '\n') {
      // you're starting a new line
      currentLineIsBlank = true;
    } else if (c != '\r') {
      // you've gotten a character on the current line
      currentLineIsBlank = false;
    }
  }
}
// give the web browser time to receive the data
delay(1);
client.stop();

1 个答案:

答案 0 :(得分:1)

检测到错误的第4行如下:

client.println("<TEMP/>");

关闭Xml标记的正确语法是:

client.println("</TEMP>");
  

注意:语法<TEMP/>用于声明空属性。它相当于<TEMP></TEMP>