繁体   English   中英

PHP DOMDocument :: loadXML与utf-8 xml输入

[英]PHP DOMDocument::loadXML with utf-8 xml input

对于跟随代码,当我把

$doc = new DomDocument('1.0');

我在DOMDocument :: 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; 
    }   

loadXML不是静态函数,因此像DOMDocument::loadXML这样调用它将是错误的。 您应该使用dom实例: $doc->loadXML($xml);

否则,您的代码就很好,并产生了它的需求-我没有测试xsl的东西,因为我不知道应该在$XsltFileName

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM