阅读Oracle存储过程并将其表示为XML

时间:2012-01-15 23:22:31

标签: php oracle stored-procedures simplexml

我想做以下事情:创建Oracle存储过程以读取我尝试使用SELECT * FROM base的数据库中的所有数据;但这不适用于Oracle。 但也许我需要做的更多不同,因为我想做这样的事情: 我想让Procedure返回数据的XML表示: 这是我用于制作XML文档的代码,我的问题是如何更改它,以便我可以使用此代码从存储过程发送数据。

代码:

require_once('test_xml.php');
$library = array(
    'book' => array(
        array(
            'authorFirst' => 'Mark',
            'authorLast' => 'Twain',
            'title' => 'The Innocents Abroad'
        ),
        array(
            'authorFirst' => 'Charles',
            'authorLast' => 'Dickens',
            'title' => 'Oliver Twist'
        )
    )
);
$ArrayToXml=new ArrayToXml();
echo $ArrayToXml->toXml($library);

制作XML的课程:

class ArrayToXML
{
    /**
     * The main function for converting to an XML document.
     * Pass in a multi dimensional array and this recrusively loops through and builds up an XML document.
     *
     * @param array $data
     * @param string $rootNodeName - what you want the root node to be - defaultsto data.
     * @param SimpleXMLElement $xml - should only be used recursively
     * @return string XML
     */
    public static function toXml($data, $rootNodeName = 'data', &$xml=null)
    {
        // turn off compatibility mode as simple xml throws a wobbly if you don't.
        if (ini_get('zend.ze1_compatibility_mode') == 1)
        {
            ini_set ('zend.ze1_compatibility_mode', 0);
        }

        if (is_null($xml))
        {
            $xml = simplexml_load_string("<".key($data)."s/>");
        }

        // loop through the data passed in.
        foreach($data as $key => $value)
        {
            // if numeric key, assume array of rootNodeName elements
            if (is_numeric($key))
            {
                $key = $rootNodeName;
            }

            // delete any char not allowed in XML element names
            $key = preg_replace('/[^a-z0-9\-\_\.\:]/i', '', $key);

            // if there is another array found recrusively call this function
            if (is_array($value))
            {
                // create a new node unless this is an array of elements
                $node = ArrayToXML::isAssoc($value) ? $xml->addChild($key) : $xml;

                // recrusive call - pass $key as the new rootNodeName
                ArrayToXML::toXml($value, $key, $node);
            }
            else
            {
                // add single node.
                $value = htmlentities($value);
                $xml->addChild($key,$value);
            }

        }
        // pass back as string. or simple xml object if you want!
        return $xml->asXML();
    }

    // determine if a variable is an associative array
    public static function isAssoc( $array ) {
        return (is_array($array) && 0 !== count(array_diff_key($array, array_keys(array_keys($array)))));
    }
}

请帮助我不知道如何将其实现到存储过程的SELECT *语句中。

谢谢

1 个答案:

答案 0 :(得分:0)

您可以从整个数据库中读取所有数据。您必须为每个表创建单独的“select”语句。