public static function array_to_xml(XMLElement $parent, array $data, $validate = false)
    {
        foreach ($data as $element_name => $value) {
            if (!is_numeric($value) && empty($value)) {
                continue;
            }

            if (is_int($element_name)) {
                $child = new XMLElement('item');
                $child->setAttribute('index', $element_name + 1);
            } else {
                $child = new XMLElement($element_name, null, array(), true);
            }

            if (is_array($value) || is_object($value)) {
                self::array_to_xml($child, (array)$value);

                if ($child->getNumberOfChildren() == 0) {
                    continue;
                }
            } elseif ($validate === true && !self::validateXML(self::sanitize($value), $errors, false, new XSLTProcess)) {
                continue;
            } else {
                $child->setValue(self::sanitize($value));
            }

            $parent->appendChild($child);
        }
    }