greenheart Posted October 16, 2009 Share Posted October 16, 2009 Hello, I am parsing an xml file from an API which I have converted into a DOMDocument in php. This is mostly fine but one problem I have is when I do this: $feeditem->getElementsByTagName('extra'); as part of a forall statment and the element extra doesn't exist in one of the feeditems I am iterating through in the forall condition then I get an error. I think I need some kind of statment like: if ($feeditem->getElementsByTagName('extra') ISN'T NULL) But don't know how to formulate it. Please help. Thanks. Link to comment https://forums.phpfreaks.com/topic/177963-domdocument-parsing/ Share on other sites More sharing options...
chronister Posted October 16, 2009 Share Posted October 16, 2009 I think you mean foreach.... foreach($this as $that){ if (!empty($feeditem->getElementsByTagName('extra')){ // do something here. } } Link to comment https://forums.phpfreaks.com/topic/177963-domdocument-parsing/#findComment-938312 Share on other sites More sharing options...
teynon Posted October 16, 2009 Share Posted October 16, 2009 if (!is_null($feeditem->getElementsByTagName('extra'))) Link to comment https://forums.phpfreaks.com/topic/177963-domdocument-parsing/#findComment-938313 Share on other sites More sharing options...
greenheart Posted October 16, 2009 Author Share Posted October 16, 2009 Yes Chronister I meant foreach. I tried your code and it gave an error but thanks for trying. Tenyon yours worked so thanks. Prob solved. Link to comment https://forums.phpfreaks.com/topic/177963-domdocument-parsing/#findComment-938333 Share on other sites More sharing options...
chronister Posted October 16, 2009 Share Posted October 16, 2009 yeah, mine has a syntax error.... Should be .... foreach($this as $that){ if (!empty($feeditem->getElementsByTagName('extra'))){ // do something here. } } Link to comment https://forums.phpfreaks.com/topic/177963-domdocument-parsing/#findComment-938337 Share on other sites More sharing options...
greenheart Posted October 16, 2009 Author Share Posted October 16, 2009 SORRY I was wrong, I haven't got it working. basically when 'extra' isn't detected by getelementsbetagname I get a non-object error trying Tenyon's and "Can't use method return value in write contex" with yours Chronister. So I need the if statement to specify that it isn't a non-object, I think? Link to comment https://forums.phpfreaks.com/topic/177963-domdocument-parsing/#findComment-938355 Share on other sites More sharing options...
teynon Posted October 16, 2009 Share Posted October 16, 2009 Your going to have to post more of your script for us to know what else is going on or better understand what you are checking for. Link to comment https://forums.phpfreaks.com/topic/177963-domdocument-parsing/#findComment-938358 Share on other sites More sharing options...
greenheart Posted October 16, 2009 Author Share Posted October 16, 2009 Sorry here's the code chunk: if (!empty($feeditem->getElementsByTagName('extra'))){ $extra = $feeditem->getElementsByTagName('extra'); $extraname = $extra->item(0)->getAttribute('name'); } Code works fine if the DOMdocument has an 'extra' element in each of the $feeditems. Your advice is really appreciated. Link to comment https://forums.phpfreaks.com/topic/177963-domdocument-parsing/#findComment-938364 Share on other sites More sharing options...
greenheart Posted October 17, 2009 Author Share Posted October 17, 2009 Anyone? Link to comment https://forums.phpfreaks.com/topic/177963-domdocument-parsing/#findComment-938792 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.