Arkane Posted October 3, 2009 Share Posted October 3, 2009 Hey, I've made a script to get data from another page which uses DOMDocument. It works, but only if all of the tags exist. For instance, I use $var = $dom->getElementById('ctl00_mainContent_identityStrip_lblRank')->nodeValue; The problem is, that the page I'm taking it from doesn't always have the element with that id. I'm checking for multiple on the page, but some are there and some arent, but the script breaks if they aren't all there. How can I check if the node exists, and then only create the variable if it does? Quote Link to comment https://forums.phpfreaks.com/topic/176425-checking-if-a-node-exists/ Share on other sites More sharing options...
salathe Posted October 3, 2009 Share Posted October 3, 2009 DOMDocument::getElementById returns NULL if there is no node with that ID, so check for NULL before trying to access any properties (like nodeValue). Quote Link to comment https://forums.phpfreaks.com/topic/176425-checking-if-a-node-exists/#findComment-929951 Share on other sites More sharing options...
Arkane Posted October 4, 2009 Author Share Posted October 4, 2009 thank you Salathe. that worked perfectly. Quote Link to comment https://forums.phpfreaks.com/topic/176425-checking-if-a-node-exists/#findComment-929966 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.