setValueForNode<T, V> method

void setValueForNode<T, V>(
  1. String tag,
  2. V? value
)

Sets the text value for a specific child node with the given tag.

If the child node does not exist, it creates a new one with the provided value. If the value is null, the child node will be removed if it exists.

Implementation

void setValueForNode<T, V>(String tag, V? value) {
  var element = findElements(tag).singleOrNull;

  if (value == null) {
    if (element != null) {
      element.remove();
      element = null;
      return;
    }
  } else {
    string svalue = changeTo(value, "");
    consoleLog("Setting $tag to $svalue");
    if (element == null) {
      // If the element does not exist, create a new one with the provided value
      final newNode = XmlElement(XmlName(tag));
      newNode.innerText = svalue;
      children.add(newNode);
    } else {
      element.innerText = svalue;
    }
  }
}