Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Problem parsing null nodes in php
Old 08-29-2007, 05:39 PM Problem parsing null nodes in php
Novice Talker

Posts: 7
Name: Glen
Trades: 0
Just a quickie that someone will probably be able to answer quickly...

I am parsing an XML doc, when the parser hits a null node, ie no date eg </nodata> my parser comes up with the a fatal error (Call to a member function on a non-object)

Here is a snippet of the parser, the last line causes the error.

foreach ($element->child_nodes() as $e) {
if (isset($e->tagname)) {
$element=$e->tagname;
$node=$e->first_child();
$data=htmlspecialchars($node->node_value());


I have tried various methods of trying to bypass the line depending on if the node_value is null, but nothing seems to work....I guess I must be missing something here as I am not that good with XML...

Sadly I am limited by the server and can only use PHP4 (so can't get version 5 XML goodies :-( !!!

Please, can anyone suggest anything?

Thanks.

Glen
poachedeggs is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-29-2007, 06:26 PM Re: Problem parsing null nodes in php
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
As you don't have the try{} catch available, did you simply tried to check with an if() that the first_child() haven't returned a null value?
http://www.php.net/manual/en/functio...irst-child.php
Quote:
(PHP >= 4.3 only) If no first child is found, NULL is returned.
PHP Code:
foreach ($element->child_nodes() as $e) {
  if (isset(
$e->tagname)) {
    
$element=$e->tagname;
    
$node=$e->first_child();
    if(
$node!==NULL){
      
$data=htmlspecialchars($node->node_value());
    }
  }

__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 08-29-2007, 06:33 PM Re: Problem parsing null nodes in php
Novice Talker

Posts: 7
Name: Glen
Trades: 0
....cheers, I owe you a beer!!

I was using whether if (empty)...and that still caused a problem, but I've just used your suggestion Thierry and works just fine...I guess one of those things that when you are stuck you just can't see the simplest solution...

Muchas gracias amigo,

Glen
poachedeggs is offline
Reply With Quote
View Public Profile
 
Old 08-29-2007, 07:13 PM Re: Problem parsing null nodes in php
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
I'm up for the beer !
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Reply     « Reply to Problem parsing null nodes in php
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.19824 seconds with 12 queries