PHP DOMDocument :: loadXML,带有utf-8 xml输入

时间:2013-04-21 14:07:05

标签: php dom

对于以下代码,当我把

$doc = new DomDocument('1.0');

我收到了警告 DOM文档:: saveXML() xmlEscapeEntities:char超出范围

当我把

$doc = new DomDocument('1.0','UTF-8');

我在$ xmldoc = DOMDocument :: loadXML($ xml)中得到了错误的值;

有代码

function readXsltTemplateAdmin($XsltFileName,$arrData,$params="",$other="",$number="")
    {

        //      kreate XML from array
        /** primer:
        * $xml = <<<EOB
        * <alldata>
        *  <datarow>
        *   <name>sasa</name>
        *   <comment>aab</comment>  
        *  </datatrow>
        *  <datatrow>
        *   <name>dragana</name>
        *   <comment>eeeee</comment>  
        *  </commentrow>
        * </alldata>
        * EOB;
        */

        $doc = new DomDocument('1.0','UTF-8');
        //$doc = new DomDocument('1.0');

        // root 
        $root = $doc->createElement('alldata'.$number);
        $root = $doc->appendChild($root);

        if (is_array($other) || is_array($params))
        {
            if($other!="")
            {   
                foreach($other as $k => $v)     
                {
                      $child = $doc->createElement($k);
                      $child = $root->appendChild($child);
                      $value = $doc->createTextNode($v);
                      $value = $child->appendChild($value);                         
                }
            }

            foreach($params as $ParamKey => $ParamValue)        
            {
                $child = $doc->createElement($ParamKey);
                $child = $root->appendChild($child);
                $value = $doc->createTextNode($ParamValue);
                $value = $child->appendChild($value);                           
            }           

        }   

        if (sizeof($arrData)>0)
        {
            for ($i=0;$i<sizeof($arrData);$i++) {

                $occ = $doc->createElement("datarow");
                $occ = $root->appendChild($occ);
                foreach ($arrData[$i] as $fieldname => $fieldvalue) {

                $child = $doc->createElement($fieldname);
                $child = $occ->appendChild($child);


                $value = $doc->createTextNode($fieldvalue);
                $value = $child->appendChild($value);
              } 
            } 
        }

        $xml = $doc->saveXML();

        $handle = fopen($XsltFileName, "r");

        $xsl = fread($handle, filesize($XsltFileName)) or die();

        fclose($handle);    

        $xmldoc = DOMDocument::loadXML($xml);
        $xsldoc = DOMDocument::loadXML($xsl);

        $proc = new XSLTProcessor() ;
        $proc->registerPHPFunctions();
        $proc->importStyleSheet($xsldoc);

        $output=$proc->transformToXML($xmldoc);         
        return $output; 
    }   

1 个答案:

答案 0 :(得分:0)

loadXML不是静态函数,因此调用DOMDocument::loadXML会出错。 你应该使用dom实例:$doc->loadXML($xml);

除此之外,你的代码很好并且产生了它的需求 - 我没有测试xsl的东西,因为我不知道应该在$XsltFileName中的内容