private function &convert($nodeName, $arr = [])
    {
        /** @var \DOMDocument $xml */
        $xml = $this->xml;

        /** @var \DOMElement $node */
        $node = $xml->createElement($nodeName);

        if (is_array($arr)) {

            // get the attributes first.;
            if (isset($arr['@attributes'])) {

                $this->extractAttributes($node, $arr, $nodeName);

                unset($arr['@attributes']); //remove the key from the array once done.
            }

            // Check if it has a value stored in @value, if yes store the value and return
            // else check if its directly stored as string.
            if (isset($arr['@value'])) {

                $node->appendChild($xml->createTextNode($this->boolToString($arr['@value'])));
                unset($arr['@value']);    //remove the key from the array once done.

                //Return from recursion, as a note with value cannot have child nodes.
                return $node;

            } elseif (isset($arr['@cdata'])) {

                $node->appendChild($xml->createCDATASection($this->boolToString($arr['@cdata'])));
                unset($arr['@cdata']);    //remove the key from the array once done.

                //Return from recursion, as a note with cdata cannot have child nodes.
                return $node;
            }

            // recurse to get the node for that key
            foreach ($arr as $key => $value) {

                if (! $this->isValidTagName($key)) {

                    throw new \Exception(__CLASS__.' Illegal character in tag name. tag: '.$key.' in node: '.$nodeName);

                }

                if (is_array($value) && is_numeric(key($value))) {

                    // MORE THAN ONE NODE OF ITS KIND;
                    // if the new array is numeric index, means it is array of nodes of the same kind
                    // it should follow the parent key name
                    foreach ($value as $k => $v) {

                        $node->appendChild($this->convert($key, $v));

                    }

                } else {

                    // ONLY ONE NODE OF ITS KIND
                    $node->appendChild($this->convert($key, $value));

                }

                unset($arr[$key]); //remove the key from the array once done.
            }

        } else {

            $node->appendChild($xml->createTextNode($this->boolToString($arr)));

        }

        return $node;
    }