tags:

views:

168

answers:

2

Hi, i am using DOM to get content of div tag but inner html part is not shown. Function is:

$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTMLFile("$url");
libxml_use_internal_errors(false);
$xpath = new DOMXPath($dom);
$divTag = $xpath->query('//div[@id="post"]');
foreach ($divTag as $val) {
echo $val->getAttribute('title') . ' - ' . $val->nodeValue . "<br />\n";
}

if source of page is (just for Div)

<div id="post">Some text <img src="..." /> <table>some codes</table></div>

then function returns just

"Some text "

but i want to get all HTML elements too, like that:

Some text <img src="..." /> <table>some codes</table>

Is there any way to do it? Thanks right now.

A: 

try removing the '//' from your Xpath Query...

this tells the XPath parser then to also get all child nodes... which your HTML tags are...

Reference: http://www.w3schools.com/XPath/xpath%5Fsyntax.asp

EDIT:

Also check XPath Axes: http://www.w3schools.com/xpath/xpath%5Faxes.asp

Tony
`//` tells XPath to get all nodes, no matter where in the tree. So `//div[@id="post"]` gets all div nodes with an id=post. The fact that you also get the children of the div nodes is incidental.
dnagirl
+1  A: 

If you're looking for the DOMDocument version of innerHTML in the browser DOM, the nearest is saveXML.

echo $dom->saveXML(val).'<br />\n';

(Remember to htmlspecialchars if you want that to actually appear as text.)

This gives you the outerHTML though. If you really need the innerHTML, you'd have to loop through each of the element's child nodes and pass them to saveXML, then implode them.

And it's XML serialisation only: there is no corresponding HTML version. saveHTML does exist but can only save the whole document at once, sadly. If it matters that you get legacy-HTML, you might be able to get away with it by passing in the LIBXML_NOEMPTYTAG option to ensure that annoying empty tags like <script src="..."></script> don't break the browser.

bobince
thank you, you all right.. Solution is: $dom = new DOMDocument(); libxml_use_internal_errors(true); @$dom->loadHTMLFile("$url"); libxml_use_internal_errors(false); $xpath = new DOMXpath($dom); $divTag = $xpath->evaluate("//div[@id=post]"); $divcontent = $divTag->item(0); echo $dom->saveXML($divcontent); and it works perfectly...
Alper