Write to XML file [Solved]

Hello,
I am trying to write to a XML file.

I can write some values but limited. When I add an extra line or value the xml file stop updating.

  File xml = FileSystem.open("/mnt/sda1/Data/data.xml", FILE_WRITE); // open the file
  if(xml){
    Serial.println("XML Opened");
    xml.println("<?xml version=\'1.0\' encoding=\'UTF-8\'?> ");
    xml.println("<sea>");
      xml.println("\t<temperatures>"); //ASCII control codes (TAB)
        xml.print("\t\t<water>");
          xml.print(waterTmp);
        xml.println("</water>");
      xml.println("\t</temperatures>");
    xml.println("</sea>");
    

    xml.close();
  }
  else{
    Serial.print("XML Error");
  }

For e.g if I add

 xml.print("\t\t<air>");
xml.print(airTMP);
xml.println("</air>");

the file is not created but no errors are up.

Could it be a avr memory issue? Too many strings?

well I imagined that because if this lines of code are alone in a sketch everything is good.
Another strange thing is if I add the same line twice or more times it can write them to the file.

Do you have any idea how this problem could be solved?

I would delegate the task to the linux side. Wrap the XML composition is a script (python, bash, you name it) and call the script with proper parameters (waterTmp, airTMP)

For example, you can use python Template. The template string would be your xml, with $waterTmp variables. You then call substitute(waterTmp=17) and your xml is done

Yes, this is also a good idea.
I did something similar but on my server site.

Write the data to .txt then upload it to my server, after that read the file with php and create a new xml file.

Thank you Federico

Assuming that the receiving end of the xml() object in your sketch is a Pythin script, you might want to check that that one is started with the -u option to disabled buffered output. Usually only effects writes/prints to stdout but IMPE, you never know with Python... :grin:

Ralf :wink: