protected function XMLForLocations($locations)
    {
        $XML = new \DOMDocument();
        $locationsElement = $XML->createElement('locations');
        $XML->appendChild($locationsElement);

        foreach ($locations as $location) {
            $locationElement = $XML->createElement('location');
            $locationsElement->appendChild($locationElement);

            // copy attributes
            if (array_key_exists('attrs', $location)) {
                foreach ($location['attrs'] as $attributeName => $attributeContent) {
                    $locationElement->setAttribute($attributeName, $attributeContent);
                }
            }

            // copy child elements
            if (array_key_exists('ch', $location)) {
                foreach ($location['ch'] as $fieldName => $fields) {
                    foreach ($fields as $field) {
                        $childElement = $XML->createElement($fieldName);
                        $locationElement->appendChild($childElement);
                        $childElement->appendChild($XML->createTextNode($field['values'][0]));

                        // copy attributes of child elements
                        if (array_key_exists('attr', $field)) {
                            foreach ($field['attr'] as $attributeName => $attributeContent) {
                                $childElement->setAttribute($attributeName, $attributeContent);
                            }
                        }
                    }
                }
            }
        }

        return $XML;
    }