22 Aralık 2009 Salı

XML Dosyasından Veri Alma

Reklam Yöneticimizin en önde yeteneklerinden biri olacak olan şema okuma işlemi için şemaların XML formatından gelen tanımlarının okunarak bir veri yapısında tutulması gerekiyor. Bunun için yine PHP'de domdocument() nesne kütüphanesini kullanarak hazırladığımız XML sınıfına eklenti hazırladım. Eklenti kodu aşağıdaki gibi geliştirildi:

public function toArray(DOMNode$oDomNode = null)
    {
        // return empty array if dom is blank
        if (is_null($oDomNode) && !$this->doc->hasChildNodes()) {
            return array();
        }
        $oDomNode = (is_null($oDomNode)) ? $this->doc->documentElement : $oDomNode;
        if (!$oDomNode->hasChildNodes()) {
            $mResult = $oDomNode->nodeValue;
        } else {
            $mResult = array();
            foreach ($oDomNode->childNodes as $oChildNode) {
                // how many of these child nodes do we have?
                // this will give us a clue as to what the result structure should be
                $oChildNodeList = $oDomNode->getElementsByTagName($oChildNode->nodeName);
                $iChildCount = 0;
                // there are x number of childs in this node that have the same tag name
                // however, we are only interested in the # of siblings with the same tag name
                foreach ($oChildNodeList as $oNode) {
                    if ($oNode->parentNode->isSameNode($oChildNode->parentNode)) {
                        $iChildCount++;
                    }
                }
                $mValue = $this->toArray($oChildNode);
                $sKey = ($oChildNode->nodeName {0}== '#') ? 0 : $oChildNode->nodeName;
                $mValue = is_array($mValue) ? $mValue[$oChildNode->nodeName] : $mValue;
                // how many of thse child nodes do we have?
                if ($iChildCount > 1) { // more than 1 child - make numeric array
                    $mResult[$sKey][] = $mValue;
                } else {
                    $mResult[$sKey] = $mValue;
                }
            }
            // if the child is < foo>bar< /foo>, the result will be array(bar)
            // make the result just 'bar'
            if (count($mResult) == 1 && isset($mResult[0]) && !is_array($mResult[0])) {
                $mResult = $mResult[0];
            }
        }
        // get our attributes if we have any
        $arAttributes = array();
        if ($oDomNode->hasAttributes()) {
            foreach ($oDomNode->attributes as $sAttrName => $oAttrNode) {
                // retain namespace prefixes
                $arAttributes["@{$oAttrNode->nodeName}"] = $oAttrNode->nodeValue;
            }
        }
        // check for namespace attribute - Namespaces will not show up in the attributes list
        if ($oDomNode instanceof DOMElement && $oDomNode->getAttribute('xmlns')) {
            $arAttributes["@xmlns"] = $oDomNode->getAttribute('xmlns');
        }
        if (count($arAttributes)) {
            if (!is_array($mResult)) {
                $mResult = (trim($mResult)) ? array($mResult) : array();
            }
            $mResult = array_merge($mResult, $arAttributes);
        }
        $arResult = array($oDomNode->nodeName => $mResult);
        return $arResult;
    }

Bu kodun test edilmesinde kullandığım örnek kod ise;

<?php

    /**
     * The tester of the XML domdocument toArray method.
     * 
     * @author Behçet MUTLU
     * @copyright 2009
     */
     
    
    require_once("CArray2xml2array.class.php");
    
    $xml = new CArray2xml2array();
    
    if($xml->setXMLFile("library/file.xml"))
        echo "< p>XML file, < i>\"" . $xml->getXMLFile() . "\"< /i> loaded successfully.< /p>";
    else
    {
        echo "< p>Error: XML file could not be loaded!< /p>";
        exit();
    }
    
    echo "< pre>";
    print_r($xml->toArray());
    echo "< /pre>";



?>

Bu örnek kodun verdiği çıktı da şu şekildeydi:

XML file, "library/file.xml" loaded successfully.

Array
(
    [movie] => Array
        (
            [0] => 

            [clip] => Array
                (
                    [0] => Array
                        (
                            [@id] => upState
                            [@import] => up.png
                        )

                    [1] => Array
                        (
                            [@id] => downState
                            [@import] => down.png
                        )

                    [2] => Array
                        (
                            [@id] => overState
                            [@import] => over.png
                        )

                    [3] => Array
                        (
                            [@id] => disabledState
                            [@import] => disabled.png
                        )

                )

            [frame] => Array
                (
                    [0] => 
  
                    [library] => Array
                        (
                            [0] => 
    
                            [clip] => Array
                                (
                                    [0] => 
      
                                    [frame] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [0] => 
        
                                                    [place] => Array
                                                        (
                                                            [@id] => upState
                                                            [@depth] => 1
                                                        )

                                                    [stop] => 
                                                    [@name] => Up
                                                )

                                            [1] => Array
                                                (
                                                    [0] => 
        
                                                    [place] => Array
                                                        (
                                                            [@id] => downState
                                                            [@depth] => 2
                                                        )

                                                    [stop] => 
                                                    [@name] => Down
                                                )

                                            [2] => Array
                                                (
                                                    [0] => 
        
                                                    [place] => Array
                                                        (
                                                            [@id] => overState
                                                            [@depth] => 3
                                                        )

                                                    [stop] => 
                                                    [@name] => Over
                                                )

                                            [3] => Array
                                                (
                                                    [0] => 
        
                                                    [place] => Array
                                                        (
                                                            [@id] => disabledState
                                                            [@depth] => 4
                                                        )

                                                    [stop] => 
                                                    [@name] => Disabled
                                                )

                                        )

                                    [@id] => testButton
                                )

                        )

                )

            [place] => Array
                (
                    [@id] => testButton
                    [@depth] => 1
                )

            [@width] => 320
            [@height] => 240
            [@framerate] => 12
        )

)

0 yorum:

Yorum Gönder