Using XMLReader and getting a bit stuck – PHP

  php

Q(Question):

Well, the time has come for me to ask another question, I hope someone can help.

Here’s the deal, I need to read a specific node from an XML message that is stored in a string. I figure XMLReader is the way to go.

I know that the node in question will contain either ‘True’ or ‘False’ which is perfect for my project.

Here’s my code


$this->lcNZResponse = $loRequest->getResponseBody();
$loXMLReader = new XMLReader() ;
$loXMLReader->xml($this->lcNZResponse) ;
$lcTestString = $loXMLReader->getAttribute("Message") ;
if(strToLower($lcTestString) == "true")
{
$llReturn = true ;
}
return $llReturn ;

I know the response has a node in it called ‘Message’ but I can’t seem to get the value out of it.

I have run the return through intVal() which for true should return 1, but even when I know the XML node contains ‘True’ the return is always false.

I hope that makes sense, and I hope someone can help me.

Cheers
nathj

A(Answer):

Wish I could be of help, but I can’t! Never used XMLReader, and looking at it’s documentation, I can see why people wouldn’t: there’s barely any documentation!

Maybe someone else will be of service.

A(Answer):

@Markus

That part is a bit of a pain. do you have any alternative suggestions?

Cheers
nathj

A(Answer):

Well, I would really like to use the XMLReader stuff as it seems the proper way to do things. But the documentation baffles me and I can’t get it to work. so I have resorted to testing the XML like a simple string. Using strpos() I am able to determine if the bit I want is there.

It may be crude, it may not be transferable but at present it works and that’s my main aim.

If anyone does know how to use the XMLReader stuff then please let me know, I hate using a fudge and that’s what my solution feels like.

Cheers
nathj

A(Answer):

you could use SimpleXML (so you can use XPath).

regards

A(Answer):

I nice idea, I’ll have a play around with that as it could make the code much more transferable – always good when you work on multiple projects.

A(Answer):

note: SimpleXML is of no use when you have namespaces around (well if I remember right) then you have to use DOMDocument or related.

regards

LEAVE A COMMENT