views:

269

answers:

3

Hi,

i just want to know if there are any alternatives to simpleXML for parsing XML Data with PHP.

For example if simpleXML module is not loaded or even if there is a lib/class out there that has a better performance then SimpleXML.

A: 

Well there is XML_Parser (see http://php.net/manual/en/book.xml.php) aswell as XMLReader / XMLWriter (http://www.php.net/manual/en/book.xmlreader.php / http://www.php.net/manual/en/book.xmlwriter.php). SimpleXML is compiled into php per default (at least since 5.x). I can't tell you much about performance of XMLReader/XMLWriter or XML_Parser as I usually stick to SimpleXML.

Cheers,
Fabian

halfdan
+1  A: 

You can use the DOM extension. It has the advantage many people are already familiar with DOM (coming from e.g. Javascript). Of course, DOM is very painful.

For reading large XML files, the event model (think SAX) is a necessity. See here.

Artefacto
I'd say that using the DOM library is very *verbose*, but once you get used to it, it's pretty easy.
nickf
DOM and SimpleXML are two faces of the same coin, libxml. It wouldn't make any kind of sense to enable DOM and not enable SimpleXML.
Josh Davis
@Josh Davis It wouldn't, but it's technically possible.
Artefacto
Of course that's possible, but if it ever happens on any webhost it'll be the result of a bug or an oversight, which would eventually get fixed. It's not a reason to actually look for alternatives. It's like looking for an alternative to substr() in case the function was disabled.
Josh Davis
A: 

Obviously there's a ton of different way to process XML both as PHP extensions and userspace librairies. The problem is they are all much much more complicated than SimpleXML and nowhere as fast for random access.

I'm not sure what's the goal of your question though. None of those libraries/extensions share a common API so if you want a fallback in case SimpleXML isn't available then you'll have to duplicate your efforts. In actuality though, there's virtually no reason to disable SimpleXML so there's no reason to work on such a contingency plan.

Josh Davis
thats what i wanted to know ;)
choise