473,491 Members | 3,350 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 2195
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
4032
by: keepyourstupidspam | last post by:
Hi, I am using xerces dom C++, I want to change an element value, here is the code I am using but the element in the file is not getting updated. ... ... ... xercesc_2_4::DOMDocument*...
1
1773
by: Dica | last post by:
hi all first off, i'm not trying to cross post, but couldn't find this newsgroup earlier (got here from a recommendation on microsoft.public.vb, where i originally posted this question). ...
1
2549
by: Lucky | last post by:
Hi guys! i've made one Regx that extract values from xml nodes. the problem is with the node that has CDATA. XML DATA: <tag1>qfa f453q4tw34656</tag1> <tag2>vv4642663735673</tag2> <Text>...
1
8004
by: .nLL | last post by:
hi everyone i am trying to read a nodes ( i dont know if i am right by calling it node. see sample) value with asp. xml page is very simple : <?xml version="1.0" ?<response success="true"...
3
1972
by: luthriaajay | last post by:
I need to get the value from the element <Code> in Java <?xml version="1.0" encoding="UTF-8"?> <Underlying><Code>KGF</Code></Underlying> My Java code is:
1
1945
by: chittasuman | last post by:
QUESTION: There is a xml node like <icc> <cricket> <player>sachin</player> <score>15</score> </cricket> <cricket> <player>dravid</player>
7
18392
by: messagewalker | last post by:
Hi guys, I wanted to know how to set node value using xpath, for example <root> <name></name> </root> what I want is get the node 'name' and set its value to say 'bob', so it will be like...
1
1637
NawazAhmed
by: NawazAhmed | last post by:
Hi, I am just trying to get the node value from the xml file "books.xml". I am getting error: Object Required at line 12. What can be the solution for this??? This is my script <HTML> ...
8
14914
by: =?Utf-8?B?WVhR?= | last post by:
I want to do the multi-language program, save the language text in XML file, but how to read the specified node value? the xml is below, for example, i want to get the value(AAA content) that named...
2
2961
by: prao2005 | last post by:
My XML Code <DBE:Attribute name="Test1" type="Table"> <DBE:Table> <DBE:TableHeader> <DBE:TableColumn>t1</DBE:TableColumn> ...
0
7118
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
6862
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7364
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5452
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4886
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4579
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1397
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
282
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.