PHP XML access problem

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Okay, I'm trying to make an AJAX-type application that uses an
XMLHttpRequest to a PHP page which processes an XML document, getting
elements, and printing them out.  When I run it through, the only
errors that I get are 2 PHP errors from the PHP page that accesses the
XML document (since there is another PHP page I use that auto-
generates the AJAX application).  They are the following (verbatim):

Warning: DOMDocument::loadXML() [function.DOMDocument-loadXML]: Empty
string supplied as input in /usr/local/lsws/DEFAULT/html/html/
memberaccess.php5 on line 11

Fatal error: Call to undefined method
DOMNodeList::getElementsByTagName() in /usr/local/lsws/DEFAULT/html/
html/memberaccess.php5 on line 17

The php5 tag was just a test to see if my web server was interpreting
them as PHP4, since there was a reported bug in PHP4.1 that related to
these error messages.

The code for the first error is:

The second error I did a bit of troubleshooting with, so I'll
basically give a chronological version of that:
$ranks = $dom->getElementsByTagName('RANK');
$members1 = $ranks->getElementsByTagName('MEMBER1');

then I removed the first line there, and changed the second to the
$members1 = $dom->getElementsByTagName('MEMBER1');

I got the Fatal error with both of those combinations.

Thank you in advance.  Very much appreciated.

Re: PHP XML access problem

On Tue, 18 Mar 2008 22:56:38 +0100, Rankin  =

Quoted text here. Click to load it

 From the manual:
DOMDocument->load() - Load XML from a file
DOMDocument->loadXML() - Load XML from a string

So, either something like:

Quoted text here. Click to load it

Can't be done like that, DOMNodeList doesn't have a getElementsByTagName=

method. If you want to search all 'MEMBER1' tags in 'RANK' tags, I'd  =

advise using XPath.

$xpath = new DOMXPath($dom);
$members1 = $xpath->query('//RANK//MEMBER1');

You could offcourse loop though all members of $ranks, and perform the  =

method on the DOMElement. Something like this (untested):
$members = array();
for($i = 0,$ranks = $dom->getElementsByTagName('RANK');$rank =  =

$ranks->item($i); $i++)
    for($j = 0,$members_in_rank =  =

$rank->getElementsByTagName('MEMBER1');$member =  
        $members[] = $member;

Quoted text here. Click to load it

Should work, als long as $dom is either a DOMDocument or DOMElement. If =

you get a fatal error, var_dump($dom) to see what it is, and check for  =

probably an accidental reassignment making $dom something else then a  =


Also be VERY aware of the fact that PHP's DOM is absolutely NOT the same=

as PHP's DOM XML (the first one would be preferable, in which case the  =

starting DOM is written all uppercase, the second one usually is written=

with only the D capitalised (DomDocument is DOM XML, DOMDocument is DOM)=
-- =

Rik Wasmus

Re: PHP XML access problem

Quoted text here. Click to load it

Ah, that may explain a lot.  Thank you.

Site Timeline