【发布时间】:2011-11-14 15:59:59
【问题描述】:
我有一段 PHP 代码。
PHP:
$Implementation = new DOMImplementation();
$Document = $Implementation->createDocument( NULL, NULL, $Implementation->createDocumentType( 'html' ) );
$Document->encoding = 'utf-8';
$Document->loadXML( $main_html[ 'main' ] ); // load main.html
$Document->xinclude();
$Fragment = $Document->createDocumentFragment();
$Fragment->appendXML( $main_html[ 'page' ] ); // load page.html
$Document->getElementById( 'content' )->appendChild( $Fragment );
...
除了最后一行,一切正常,出现错误:
PHP Fatal error: Call to a member function appendChild() on a non-object
似乎getElementById() 方法不适用于$Document。
查看 HTML。
HTML (main.html):
...
</head>
<body xmlns:xi="http://www.w3.org/2001/XInclude">
<xi:include href="wrapper.html" />
</body>
</html>
HTML (wrapper.html):
<section
id="wrapper"
xmlns:xi="http://www.w3.org/2001/XInclude">
<xi:include href="header.html" />
<xi:include href="nav.html" />
<xi:include href="content.html" />
<xi:include href="aside.html" />
<xi:include href="footer.html" />
</section>
HTML(内容.html):
<section id="content" />
我在$Document->loadXML( $main_html[ 'main' ] ); 之前添加了$Document->validateOnParse = TRUE;,并在没有XInclude 的情况下进行了测试,但不起作用。
终于找到了解决办法,坏行替换为:
$Document->getElementsByTagName( 'section' )->item( 1 )->appendChild( $Fragment );
getElementsByTagName() 方法适用于$Document,但不能满足我。我错过了什么吗?
【问题讨论】:
标签: php dom getelementbyid