Append and prepend text nodes to a HTML element using DOM?

Here is my HTML code

<html> 
    <body>
        <div>A sample block <div>and child block</div></div>    
    </body>
</html>

How can I use DOM to append and prepend text nodes to the BODY elements without hurting its siblings?

$dom = new DOMdocument();    
@$dom->loadHTML($html);    
$xpath = new DOMXPath($dom);    
$body = $xpath->query('//body')->item(0);    

like this

<html> 
    <body>
        Newly prepended text
        <div>A sample block <div>and child block</div></div>
        Newly appended text    
    </body>
</html>  
17
задан Felix Kling 25 December 2010 в 11:19
поделиться