Hi, folks.
I've encountered what seems to me to be something of an oddity while playing
around with XML parsing in PHP, and I wondered if any of you might be able
to clear up my confusion...
Here's a little code:
$xmlDoc = new DOMDocument();
$xmlDoc->load('widget_d ata.xml');
$widgets = $xmlDoc->getElementsByT agName('widget' );
My understanding was that '$widgets' is an array of elements, and the
following 'foreach' iterates through that array (this works):
foreach ($widgets as $widget)
{
....
}
However, I get an error if I try to access '$widgets' using square-brackets,
e.g.:
$widget = $widgets[0];
(The error is: "Fatal error: Cannot use object of type DOMNodeList as
array".)
PHP confirms that the first element of the 'array' has an index/key of '0'
with the following (or similar), but won't let me access it directly using
'$widgets[0]':
foreach ($widgets as $key =$widget)
{
echo("<p>" . $key . "</p>\n");
}
Does anyone have an explanation as to why a DOMNodeList can be accessed like
an array using 'foreach', but won't allow square-brackets to be used? What
exactly *is* a DOMNodeList?
Thanks for any help.
A.
PS. I am aware that DOMNodeLists have an 'item()' method with which nodes
within the list can be referred to via index, but my confusion still stands
as to why I can't simply use '[]'s. 7 8125
On Mon, 30 Oct 2006 14:24:45 GMT, "Andrew C" <no******@total ly.made.upwrote :
>I've encountered what seems to me to be something of an oddity while playing around with XML parsing in PHP, and I wondered if any of you might be able to clear up my confusion...
Here's a little code:
$xmlDoc = new DOMDocument(); $xmlDoc->load('widget_d ata.xml'); $widgets = $xmlDoc->getElementsByT agName('widget' );
My understanding was that '$widgets' is an array of elements,
Nope - it's a DOMNodeList object, that has support for PHP5 iterators, so you
can use it in a foreach loop.
>and the following 'foreach' iterates through that array (this works):
foreach ($widgets as $widget) { ... }
However, I get an error if I try to access '$widgets' using square-brackets, e.g.:
$widget = $widgets[0];
(The error is: "Fatal error: Cannot use object of type DOMNodeList as array".)
There you go.
>PHP confirms that the first element of the 'array' has an index/key of '0' with the following (or similar), but won't let me access it directly using '$widgets[0]':
foreach ($widgets as $key =$widget) { echo("<p>" . $key . "</p>\n"); }
Does anyone have an explanation as to why a DOMNodeList can be accessed like an array using 'foreach', but won't allow square-brackets to be used? What exactly *is* a DOMNodeList?
Thanks for any help.
A.
PS. I am aware that DOMNodeLists have an 'item()' method with which nodes within the list can be referred to via index, but my confusion still stands as to why I can't simply use '[]'s.
PHP doesn't support overloading operators, which I believe would be required
for supporting [] on objects implementing the appropriate interface.
Would be a sensible extension to the current functionality, though - as you
point out, DOMNodeList acts almost the same as an array, but not quite close
enough.
Failing that, it'd be somewhat useful if DOMNodeList had a method to convert
itself into an actual array, but it doesn't seem to have that either.
--
Andy Hassall :: an**@andyh.co.u k :: http://www.andyh.co.uk http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Does anyone have an explanation as to why a DOMNodeList can be accessed like
an array using 'foreach', but won't allow square-brackets to be used? What
exactly *is* a DOMNodeList?
A DOMNodeList is an object - this is why you can't access items using
square-brackets.
Since PHP5 you can iterate through all visible properties of an object
- this is why you can access items using a foreach statement.
In article <q1************ *************** *****@4ax.com>, Andy Hassall
says...
Failing that, it'd be somewhat useful if DOMNodeList had a method to convert
itself into an actual array, but it doesn't seem to have that either.
If, as whiskey says, the nodes are properties of the object, you can do
an explicit cast of the object to an array:
$arrayofnodes = (array)$nodelis t;
--
PleegWat
Remove caps to reply
"Andy Hassall" <an**@andyh.co. ukwrote in message
news:q1******** *************** *********@4ax.c om...
On Mon, 30 Oct 2006 14:24:45 GMT, "Andrew C" <no******@total ly.made.up>
wrote:
>>I've encountered what seems to me to be something of an oddity while playing around with XML parsing in PHP, and I wondered if any of you might be able to clear up my confusion...
Here's a little code:
$xmlDoc = new DOMDocument(); $xmlDoc->load('widget_d ata.xml'); $widgets = $xmlDoc->getElementsByT agName('widget' );
My understanding was that '$widgets' is an array of elements,
Nope - it's a DOMNodeList object, that has support for PHP5 iterators, so
you
can use it in a foreach loop.
Thanks. That's my missing piece. I have quite a bit of experience of PHP
prior to 5, but I'm just feeling my way with some of the 5 stuff. :-)
A.
I dare say you can not cast a DOMNodeList to an array. Because, when
converting an object to an array, you get the properties of the object.
And DOMNodeList has only one porperty, which is "length" (and useless
in this context).
Anyway, I can't find any reason why one will ever need to convert a
DOMNodeList to an array since it provides a method to retrieve a node
specified by it's index ("item()"). Moreover, PHP5 offers the ability
to iterate through objects. So why this fuss ?
PleegWat, I never said nodes are properties. All I said was that PHP5
adds the ability to iterate the visible properties of an object. But
that's the default. You can customize the way PHP iterates through
objects by implementing the "Iterator" or "IteratorAggreg ate"
interfaces ;-)
On Oct 31, 12:27 am, PleegWat
<pleeg...@PLEEG WAT.leegwater-68.demon.nl.INV ALIDwrote:
In article <q13ck2d3tc2204 gdsi3db9q0bds51 0p...@4ax.com>, Andy Hassall
says...
Failing that, it'd be somewhat useful if DOMNodeList had a method to convert
itself into an actual array, but it doesn't seem to have that either.If, as whiskey says, the nodes are properties of the object, you can do
an explicit cast of the object to an array:
$arrayofnodes = (array)$nodelis t;
--
PleegWat
Remove caps to reply
In article <11************ **********@m73g 2000cwd.googleg roups.com>,
whiskey says...
On Oct 31, 12:27 am, PleegWat
<pleeg...@PLEEG WAT.leegwater-68.demon.nl.INV ALIDwrote:
In article <q13ck2d3tc2204 gdsi3db9q0bds51 0p...@4ax.com>, Andy Hassall
says...
Failing that, it'd be somewhat useful if DOMNodeList had a method to
convert itself into an actual array, but it doesn't seem to have that
either.
If, as whiskey says, the nodes are properties of the object, you can do
an explicit cast of the object to an array:
$arrayofnodes = (array)$nodelis t;
I dare say you can not cast a DOMNodeList to an array. Because, when
converting an object to an array, you get the properties of the object.
And DOMNodeList has only one porperty, which is "length" (and useless
in this context).
Anyway, I can't find any reason why one will ever need to convert a
DOMNodeList to an array since it provides a method to retrieve a node
specified by it's index ("item()"). Moreover, PHP5 offers the ability
to iterate through objects. So why this fuss ?
PleegWat, I never said nodes are properties. All I said was that PHP5
adds the ability to iterate the visible properties of an object. But
that's the default. You can customize the way PHP iterates through
objects by implementing the "Iterator" or "IteratorAggreg ate"
interfaces ;-)
(top-posting fixed)
Sorry, misunderstood you then. I did take a quick look at the manual but
I couldn't find anything about hte DOMNodeList class.
--
PleegWat
Remove caps to reply This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Noah Spitzer-Williams |
last post by:
Hello guys,
I'm itinerating through my array using pointers in this fashion:
image is unsigned char image
do {
cout << "image byte is: " << *image << endl;
|
by: mdub317 |
last post by:
I'm totally new to programming and I am wondering; when would be a good time
to use an array or an indexer? I want to know what types of applications
would make good use of arrays or indexers. There seems to be other ways of
doing the jobs of the two and less confusing.
The books I read don't provide good examples of situations when I would need
an array or indexer. I don't really need a definition of them as I already
have that. I...
|
by: david |
last post by:
Well, as a matter of fact I_HAD_MISSED a basic thing or two, anyway,
although Ollie's answer makes perfectly sense when dealing with
classes, it doesn't seem to me to apply as well if you have to
instantiate an array of structures; consider the following useless
code :
using System;
struct MyPointS
|
by: Kantha |
last post by:
Hi all,
I have declared an Union as follows
typedef union
{
struct interrupt_bits
{
unsigned char c_int_hs_fs_status : 1,
c_setup_intflag : 1,
|
by: ZMan |
last post by:
The following code won't compile with gcc version 3.4.2
(mingw-special).
How come?
Error: cannot convert `char (*)' to `char**'
/**********************************************************/
#include <cstdio>
#define MAX_WORD_LEN 80
#define MAX_SIZE 1000
| |
by: DJG79 |
last post by:
Hi all,
I am using an open source menu that i found and it works great, except
for one thing that when the web page is not scrolled to the very top
the drop down links will not stay visible. Has anyone else had this
sort of problem with javascript? and any ideas how to fix it would be
greatly appreciated..
I have included a copy of the code below, thanks.
/**
|
by: comp.lang.php |
last post by:
// NEW 11/27/2006: FINALLY, IF YOU ADDED OR DELETED OR DID ANY KIND OF
FORM ACTION SUCCESSFULLY, DON'T RE-DISPLAY THE NEW EXPENSE ITEMS VIA
$_POST
if ($_POST && (!is_array($leaseObj->errorArray) ||
@sizeof($leaseObj->errorArray) == 0)) {
print_r(array_keys($_POST));
@reset($_POST);
$tempPost = $_POST;
foreach ($_POST as $key =$val) if (strpos($key, 'new_') === 0)
array_remove($tempPost, $tempPost);
|
by: Mercy |
last post by:
I guess my C++ is pretty darn rusty. I was just looking over sample
C++ code for practice... and I'm kind of confused about this code
fragment:
int sector2;
int i = 3;
memset(sector2, 128+i, 512);
memset(sector2+256, 255-i, 256);
|
by: lawrence k |
last post by:
I've a form that starts off like this:
<form method="post" action="profile.php?
id=5&formName=my_story_edit.htm" enctype="multipart/form-data">
<p>Email address:<br />
<input type="text" name="totalFormInputs"
value="lawrence@krubner.com" /></p>
|
by: anfetienne |
last post by:
i have this code below that i made....it loads vars from txt file splits it then puts it into an array....once in an array it the brings the pics in from the array to create thumbnails and a larger image. my problem is i have captions to go with it and when i try to load the captions nothing happens or can be seen to be happening.
i dont know where i am going wrong as i have no output or compiled errors
var locVar = new Array();...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |