コード例 #1
0
function getXML($data)
{
    $doc = new DOMDocument();
    $doc->formatOutput = true;
    // Add whitespace to make easier to read XML
    $doc->preserveWhiteSpace = false;
    $child = generate_xml_element($doc, $data);
    if ($child) {
        $doc->appendChild($child);
    }
    $outXml = $doc->saveXML();
    $xml = new DOMDocument();
    $xml->preserveWhiteSpace = false;
    $xml->formatOutput = true;
    $xml->loadXML($outXml);
    $outXml = $xml->saveXML();
    return $outXml;
}
コード例 #2
0
ファイル: functions.php プロジェクト: AlexBaker-/Cellio
function generate_xml_element($dom, $data)
{
    if (empty($data['name'])) {
        return false;
    }
    // Create the element
    $element_value = !empty($data['value']) ? $data['value'] : null;
    $element = $dom->createElement($data['name'], $element_value);
    // Add any attributes
    if (!empty($data['attributes']) && is_array($data['attributes'])) {
        foreach ($data['attributes'] as $attribute_key => $attribute_value) {
            $element->setAttribute($attribute_key, $attribute_value);
        }
    }
    // Recursively iterate through any other items in the data array
    foreach ($data as $data_key => $child_data) {
        if (!is_numeric($data_key)) {
            continue;
        }
        $child = generate_xml_element($dom, $child_data);
        if ($child) {
            $element->appendChild($child);
        }
    }
    return $element;
}