Jump to content

Echoing a DOM element


ApocalypeX

Recommended Posts

So I've selected the element by its ID and now I want to echo it on to the page. But when I try to I get this message:Catchable fatal error: Object of class DOMElement could not be converted to string in D:*******.php on line 26Heres the code:

$doc = new DOMDocument();$doc->loadHTMLfile("http://www.bungie.net/Stats/GameStatsHalo3.aspx?gameid=1585574423&player=ApocalypeX");$game = $doc->getElementById('ctl00_mainContent_bnetpgd_pnlGameDetails');echo $game;

Link to comment
Share on other sites

That what I did before but it just gives my the text inside the element and not the whole element.Lets say the elements this:<div style="background: #333333;" id="IDofEl">Hi</div>I want the php script to echo:<div style="background: #333333;" id="IDofEl">Hi</div>

Link to comment
Share on other sites

Gotcha. I've read about this, but never done it. So this comes with no guarantee. It's something to play with, anyway:

$tmpNode = $doc->getElementById('ctl00_mainContent_bnetpgd_pnlGameDetails');$tmpDOM = new DOMDocument();$tmpDOM->appendChild($tmpDOM->importNode($tmpNode, true) );$game = $tmpDOM->saveHTML();echo $game;

Link to comment
Share on other sites

I don't like admitting this, but for this situation, SimpleXML is more suitable. If you like using DOM (like I do), you can keep doing so, but for this particular instance, you should pass control to SimpleXML, just for a moment, like so:

$doc = new DOMDocument();$doc->loadHTMLfile("http://www.bungie.net/Stats/GameStatsHalo3.aspx?gameid=1585574423&player=ApocalypeX");$game = $doc->getElementById('ctl00_mainContent_bnetpgd_pnlGameDetails');echo simplexml_import_dom($game)->asXML();

The way Deidre's Dad suggest is a pure DOM way, which is fine too. The one I'm offering is likely to be more efficient though, but I haven't made any tests, so don't take my word for it.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...