469,636 Members | 1,527 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,636 developers. It's quick & easy.

Can $node->node_value() return a null value?

Hi,

I am using the following function to access a nodes value from an XML
file. It works fine until it discovers a empty tag. I would like to
have the option of leaving some tags empty for my script.
..
..
..
function foutput_contact ($node_name) {
$xml_document = domxml_open_file('xml/contacts.xml');
if ($xml_document) {
$nodes= $xml_document->get_elements_by_tagname($node_name);
$nodeCount = count($nodes);
$node = $nodes[0];
if ($node) {
$node = $node->first_child();
return $node->node_value();
}
}
}
The error is returned at the line 'return $node->node_value()' but
only when I use an empty node. Can this script be modified to allow
empty nodes? Cheers

Burnsy
Jul 17 '05 #1
3 2063
mr_burns wrote:
The error is returned at the line 'return $node->node_value()' but
only when I use an empty node. Can this script be modified to allow
empty nodes? Cheers


Which error is returned and what is the structure of contacts.xml?
JW

Jul 17 '05 #2
"Janwillem Borleffs" <jw@jwscripts.com> wrote in message news:<41***********************@news.euronet.nl>.. .
mr_burns wrote:
The error is returned at the line 'return $node->node_value()' but
only when I use an empty node. Can this script be modified to allow
empty nodes? Cheers

Which error is returned?

Fatal error: Call to a member function on a non-object in
/home/martyn69/public_html/oarsome/includes/functions.php on line 38

and what is the structure of contacts.xml?

<?xml version="1.0" encoding="iso-8859-1"?>
<contacts>
<address1>123</address1>
<address2>Fake Street</address2>
<address3>Springfield</address3>
<phone>0141 952 3161</phone>
<mobile></mobile>
<email>bi******@yahoo.co.uk</email>
</contacts>
The error only seems to occur when I leave a node empty (see mobile
node above).

Cheers

Martyn
Jul 17 '05 #3
mr_burns wrote:
The error only seems to occur when I leave a node empty (see mobile
node above).


This should do it:

function foutput_contact ($node_name) {
$xml_document = domxml_open_file('contacts.xml');
if ($xml_document) {
$nodes= $xml_document->get_elements_by_tagname($node_name);
$nodeCount = count($nodes);
$node = $nodes[0];

if ($node && $node->has_child_nodes()) {
$node = $node->first_child();
return $node->node_value();
} else {
return "";
}
}
}

Or even simpler:

function foutput_contact ($node_name) {
$xml_document = domxml_open_file('contacts.xml');
if ($xml_document) {
$nodes= $xml_document->get_elements_by_tagname($node_name);
$nodeCount = count($nodes);
$node = $nodes[0];
return $node ? $node->get_content() : "";
}
}
JW

Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by keepyourstupidspam | last post: by
1 post views Thread by Lucky | last post: by
8 posts views Thread by =?Utf-8?B?WVhR?= | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.