Re: importNode
| From: | Sam Hobbs | Date: | Thu, 02 Sep 2004 10:38:17 +0000 |
| Subject: | Re: importNode | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-195531@lists.php.net to get a copy of this message | ||
"Sam Hobbs" <socalsam@bigfoot.com> wrote in message
news:20040902095616.71301.qmail@pb1.pair.com...
>I am trying to copy the body portion of a HTML file for use in the current
>page. I am using PHP 5.0.1.
>
> I think the following is close but not close enough; nothing gets imported
> by importNode. I assume this would be easy if I knew the DOM well enough;
> if someone else is then I hope this would be easy enough for them to
> either fix or provide something that works.
>
>
> <?php
> define("APPLICATION_ROOT", "C:/Apache2/htdocs/");
> $Filename = "Categories.html";
> if (!file_exists(APPLICATION_ROOT.$Filename)) {
> print "File $Filename does not exist<br>\n";
> exit;
> }
> $Document = new DOMDocument();
> $Document->loadHTMLFile(APPLICATION_ROOT.$Filename);
> if (!$Document) {
> echo "Error loading $Filename<br>\n";
> exit;
> }
> $NewDocument = new DOMDocument();
> $Body = $Document->getElementsByTagName("body")->item(0);
> foreach($Body->getElementsByTagName("*") as $Node)
> $NewDocument->importNode($Node, FALSE);
> print $NewDocument->saveHTML();
> ?>
Ooops, sorry. I should have searched the archives before posting. I see I
need to do an appendChild after importNode. That works if I set deep to
TRUE, except I get elements repeated. I am confident I will solve that in a
couple of minutes, so I think I am finally done with that portion.