Modifying an XML file with Dart

216
  1. The XML input you posted is invalid, you will get a parse error because <Settings> and <Alarm> have no (matching) closing tag.
  2. XmlElement.text is a readonly property, but XmlElement.innerText can be used to replace the text contents.
  3. To transform the document back to a string (and write it to a file) use document.toXmlString().
Share:
216
LBound
Author by

LBound

Updated on December 30, 2022

Comments

  • LBound
    LBound over 1 year

    I want to modify the values in an XML file in a Flutter app.

    The XML looks something like this:

    <Alarm>
        <Type>ReallyLoudAlarm</Type>
        <Issue>2</Issue>
        <Revision>5</Revision>
        <Settings>
            <AlarmVolume type="int" min="0" max="100" unit="%">80</AlarmVolume>
            <AlarmEnabled type="bool"> 1 </AlarmEnabled>
            <AlarmMessage type="str" maxLen="20">Wake up</AlarmMessage>
        </Settings>
    </Alarm>
    

    Which I am parsing with this XML parser and reading the values into my UI.

    I would just like to update the value of an element by doing something like document.findAllElements('AlarmVolume').first.text = number.toString();.

    Am I just missing the way to do this, or do I have to rebuild the entire XML file when I save my form?