Modifying an XML file with Dart
216
- The XML input you posted is invalid, you will get a parse error because
<Settings>
and<Alarm>
have no (matching) closing tag. XmlElement.text
is a readonly property, butXmlElement.innerText
can be used to replace the text contents.- To transform the document back to a string (and write it to a file) use
document.toXmlString()
.
Author by
LBound
Updated on December 30, 2022Comments
-
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?