简体   繁体   中英

PHP DOMDocument::loadXML with utf-8 xml input

For follow code, when I put

$doc = new DomDocument('1.0');

I got warning in DOMDocument::saveXML() xmlEscapeEntities : char out of range

and when I put

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

i got false value in $xmldoc = DOMDocument::loadXML($xml);

there is code

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 is not a static function so call it like DOMDocument::loadXML will be wrong. you should use the dom instance: $doc->loadXML($xml);

other then that your code is fine and producing what its needs - i didn't test the xsl stuff cause i don't know the content that should be in $XsltFileName

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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