Go Down

Topic: [SOLVED]XML not valid  (Read 4193 times) previous topic - next topic

gcp900

Aug 15, 2015, 05:29 pm Last Edit: Aug 18, 2015, 12:04 am by gcp900
Hi guys!!

I am doing a project in which I have to send an XML with arduino to a webpage for representing different values from sensors. I know there are tutorials about this on the internet but I am really stucked because of everything looks good but the XML is not recognized by the browser but it is recognized as a text file without format.

This is how the XML file looks like:
Code: [Select]
<?xml version = '1.0' encoding='UTF-8'?>

<inputs>
   <dato>
    15
   </dato>
</inputs>


And this is how the code sends the file:

Code: [Select]
void inicial(YunClient client)
{
    client.println("<?xml version = '1.0' encoding='UTF-8'?>");
    client.println("");
    client.println("<inputs>");
    client.print("   ");
    client.println("<dato>");
    client.print("    ");
    client.println(tnow[0]);
    client.print("   ");
    client.println("</dato>");
    client.println("</inputs>");
}


Did you detect any error? Any idea?

Thanks!!

SukkoPera

If you are transferring the file through http, you must send a valid http header. In that header, make sure you have text/xml (or application/xml) as your Content-Type.
"Code is read much more often than it is written, so plan accordingly. Design for readability."

Guida rapida a ESP8266: https://goo.gl/kzh62E

gcp900

#2
Aug 15, 2015, 07:31 pm Last Edit: Aug 15, 2015, 10:11 pm by gcp900
The new file:

Code: [Select]
HTTP/1.1 200 OK
Content-Type: text/xml
Connection: keep-alive

<?xml version = '1.0' encoding='UTF-8'?>
<inputs>
   <dato>
    17
   </dato>
</inputs>


That's how it looks right now but same problem, it is recognized as a text in spite of XML file.


jessemonroy650

::::SNIP::::

That's how it looks right now but same problem, it is recognaized as a text in spite of XML file.


@gcp900,
XML is a PITA (Pain in the Ass). This is the reason most people go with JSON.

Your PROBLEM could be an extra blank space, or a missing quotation mark. Hard to tell sometimes.

Luckily, there is a validator. https://validator.w3.org/

It will usually find the error.

Best of Luck.
Jesse

sonnyyu

http get or http post?




gcp900

Http post, but I've also tried with get. Could be different?

gcp900

#6
Aug 15, 2015, 10:39 pm Last Edit: Aug 15, 2015, 10:40 pm by gcp900
I have used the validator and the result is that when I put the file with headers it detects the document as an HTML 4.01 transitional and detects 2 errors. When I put the document whitout the headers it tells me that it is an XML document and everything is ok!! This is crazy!! What's happening??

Thanks for your help!

sonnyyu

Http post, but I've also tried with get. Could be different?
Can be only one case. If it is required Http post then you have to post. Your code is for http get.



Robin2

Have you another (non-Arduino) way to send the valid XML to the server?

If so can you see exactly what that sends and compare it to what your Arduino code sends ?

I have so far avoided XML but I wonder if you are confusing the content of the XML message and the "envelope" in which it is sent to the server.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

gcp900

Can be only one case. If it is required Http post then you have to post. Your code is for http get.



Ok so I will change to get mode in the AJAX code

gcp900

#10
Aug 16, 2015, 01:13 pm Last Edit: Aug 16, 2015, 01:14 pm by gcp900
Have you another (non-Arduino) way to send the valid XML to the server?

If so can you see exactly what that sends and compare it to what your Arduino code sends ?

I have so far avoided XML but I wonder if you are confusing the content of the XML message and the "envelope" in which it is sent to the server.

...R
I have programmed the XML in arduino following the tutorial of startingelectronics, that's why I am so confused of what I am doing wrong because the response is written the same way.

I will try to find a way to send an XML response as I am doing with arduino and I will write the result.

Thanks!



sonnyyu

#11
Aug 16, 2015, 02:14 pm Last Edit: Aug 16, 2015, 02:27 pm by sonnyyu
make html page at desktop, open it by browser and click submit and read response from browser.

Test Post:
Code: [Select]
<html>
    <body>
        <form action="http://target_webservice" method="post">
            <textarea rows="20" cols="100" name=" ">
                <?xml version = '1.0' encoding='UTF-8'?><inputs><dato>17</dato></inputs>
            </textarea>
            <input type="submit" value="Submit">
        </form>        
    </body>
</html>


name=" " has space!

Test Get:

Code: [Select]
<html>
    <body>
        <form action="http://target_webservice" method="get">
            <textarea rows="20" cols="100" name="variablename">
                <?xml version = '1.0' encoding='UTF-8'?><inputs><dato>17</dato></inputs>
            </textarea>
            <input type="submit" value="Submit">
        </form>        
    </body>
</html>


name="variablename"  get variablename from target_webservice  document.





PaulMurrayCbr

I have used the validator and the result is that when I put the file with headers it detects the document as an HTML 4.01 transitional and detects 2 errors. When I put the document whitout the headers it tells me that it is an XML document and everything is ok!! This is crazy!! What's happening??
What seems to be happening is that whatever you are using to send the file is already putting in the HTTP headers. This is what I'd normally expect, anyway - you'd be using a library to do that stuff. If you put that stuff at the start of an XML file, it is no longer valid XML, so the receiver is making its next best guess and treating it as (bad) HTML.

gcp900

#13
Aug 17, 2015, 04:40 pm Last Edit: Aug 17, 2015, 04:42 pm by gcp900
Finally, I FOUND THE PROBLEM!!!!

For everyone who wants to manage XML files (or HMTL files) with arduino yun, IT IS MANDATORY TO USE THIS HEADERS BEFORE PRINTING THE RESPONSE because if not it would be treated as a plain text.

client.println("Status: 200");
client.println("Content-Type: text/xml");

EXACTLY THAT WAY, and changing the content-type depending on the file we want to send. After that we would write the XML or HTML file.

Thanks for everyone!! I hope this could be helpful for other projects ;)

Robin2

Finally, I FOUND THE PROBLEM!!!!
As suggested by @SukkoPera in Reply #1 ?

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up