LBound
LBound

Reputation: 103

Modifying an XML file with Dart

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?

Upvotes: 3

Views: 1851

Answers (1)

Lukas Renggli
Lukas Renggli

Reputation: 8947

  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().

Upvotes: 2

Related Questions