updateXmlValue method

Future<void> updateXmlValue({
  1. required String filePath,
  2. required String key,
  3. required String value,
})

修改Xml值

Implementation

Future<void> updateXmlValue({
  required String filePath,
  required String key,
  required String value,
}) async {
  // const filePath = 'macos/Runner/Info.plist';
  final file = File(filePath);

  if (!await file.exists()) {
    _print('Info.plist file does not exist');
    return;
  }

  final document = XmlDocument.parse(await file.readAsString());
  final dictElement = document.findAllElements('dict').first;

  bool keyFound = false;
  for (var element in dictElement.children) {
    if (element is XmlElement &&
        element.name.local == 'key' &&
        element.innerText == key) {
      keyFound = true;
    }
    if (keyFound && element is XmlElement && element.name.local == 'string') {
      element.innerText = value;
      break;
    }
  }

  if (!keyFound) {
    _print('Key not found');
    return;
  }

  await file.writeAsString(document.toXmlString(pretty: true, indent: '  '));
}