toXml method

String toXml()

Renders the entire node, including its children, into an XML string.

Implementation

String toXml() {
  final decl = isDeclaration ? '?' : '';
  if (children.isEmpty) {
    if (text != null && text!.isNotEmpty) {
      final attrString = attributes.isEmpty ? '' : ' ${renderAttributes()}';
      return '<$tag$attrString>$text</$tag>';
    } else {
      return '<$decl$tag ${renderAttributes()}${closeTag ? " />" : "$decl>"}';
    }
  } else {
    final childXml = children.map((child) => child.toXml()).join();
    final xml = '<$decl$tag ${renderAttributes()}$decl>$childXml';
    return xml + (closeTag ? '</$tag>' : '');
  }
}