DOM文档字符错误


DOM document character error

未声明HTML文档的字符编码。如果文档包含US-ASCII范围之外的字符,则在某些浏览器配置中,文档将呈现乱码文本。页面的字符编码必须在文档或传输协议中声明。

dom创建如上所述的错误,当我在元数据和数据节点中创建新元素时,页面返回空白,如下所示:

<?php
$this->module->daftarkanJs('underscore-min.js');
$form = CJSON::decode(file_get_contents(Yii::app()->getBaseUrl(true).'/index.php/odk/api/index/id/'.$_GET['id']));
$input = CJSON::decode(file_get_contents(Yii::app()->getBaseUrl(true).'/index.php/odk/api/input/id/'.$_GET['id']));
function haveChild($id, $input_id){
    $child = CJSON::decode(file_get_contents(Yii::app()->getBaseUrl(true).'/index.php/odk/api/child/id/'.$id.'/parentId/'.$input_id));
    if($child['result']){ // jika child ada
        foreach($child['result'] as $data){
            // echo '-  <b>'.$data['input_id'].'</b><br/>';
            haveChild($id, $data['input_id']);
        }
        return true;
    }else{
        return false;
    }
}
function loop($inputResult, $id){
    $dom = new DOMDocument('1.0', 'utf-8');
    $dom->preserveWhiteSpace = false;
    $dom->formatOutput = true;
    $html = $dom->createElementNS('http://www.w3.org/2002/xforms', 'h:html');
    $html->setAttributeNS('http://www.w3.org/2000/xmlns/' ,'xmlns:h', 'http://www.w3.org/1999/xhtml');
    $html->setAttributeNS('http://www.w3.org/2000/xmlns/' ,'xmlns:ev', 'http://www.w3.org/2001/xml-events');
    $html->setAttributeNS('http://www.w3.org/2000/xmlns/' ,'xmlns:xsd', 'http://www.w3.org/2001/XMLSchema');
    $html->setAttributeNS('http://www.w3.org/2000/xmlns/' ,'xmlns:jr', 'http://openrosa.org/javarosa');
    $html = $dom->appendChild($html);
    $head = $dom->createElement('h:h');
    $head = $html->appendChild($head);
        $title = $dom->createElement('h:t', 'xxxxxxx');
        $title = $head->appendChild($title);
        $model = $dom->createElement('m');
        $model = $head->appendChild($model);
            $instance = $dom->createElement('instance');
            $instance = $model->appendChild($instance);
                $data = $dom->createElement('data');
                $data = $instance->appendChild($data);
                    $meta = $dom->createElement('meta');
                    $meta = $data->appendChild($meta);
                        $instanceID = $dom->createElement('instaceID');
                        $instanceID = $meta->appendChild($instanceID);
            $bind = $dom->createElement('bind');
            $bind->setAttribute("nodeset","/data/meta/instanceID");
            $bind = $model->appendChild($bind);
    foreach($inputResult as $data){
        if(!$data['parent_id']){ // ambil yang bukan child
            $check = haveChild($id, $data['input_id']);
            if(!$check){
                $data = $dom->createElement('data');
                $data = $instance->appendChild($data);
                $meta = $dom->createElement('meta');
                $meta = $data->appendChild($meta);
                $bind = $dom->createElement('bind');
                $bind->setAttribute("nodeset","/data/".str_replace(" ", "_", $data['name']));
                $bind = $model->appendChild($bind);
            }
        }
    }
    $body = $dom->createElement('h:b');
    $body = $html->appendChild($body);
    printf ("<pre>%s</pre>", htmlentities ($dom->saveXML()));
}
loop($input['result'], $_GET['id']);
?>

此行错误:

$data=$dom->createElement('data'(;$data=$instance->appendChild($data(;

$meta=$dom->createElement('meta'(;$meta=$data->appendChild($meta(;

您不输出XML,而是输出带有转义XML的HTML。

<pre>some escaped xml</pre>

如果将该输出视为XML,则该输出与错误消息匹配。这里没有带编码的XML声明。

剥离到DOM方法,您的源输出一个XML文档:https://eval.in/private/1507ef8a4065d0.

但是,我建议对所有名称空间的节点使用createElementNS((。像$dom->createElement('h:h');这样的调用是不明确的。

$xmlns = [ 'h' => 'http://www.w3.org/1999/xhtml' ];
$dom = new DOMDocument();
$html = $dom->appendChild(
  $dom->createElementNS($xmlns['h'], 'h:html')
);
$head = $html->appendChild($dom->createElementNS($xmlns['h'], 'h:head'));
echo $dom->saveXml();

输出:

<?xml version="1.0"?>
<h:html xmlns:h="http://www.w3.org/1999/xhtml"><h:head/></h:html>