Click to See Complete Forum and Search --> : Which XML/XSLT libraries to use in PHP 5?


especht
10-08-2008, 04:54 PM
Hello. I am trying to create an XML document in PHP that I will need to transform using XSLT. I'm a recovering ASP programmer just getting going with PHP, and I'm having some trouble determining which XML library to use.

I was trying to create dom documents using code like this:

<?php
include '../config/config.php';
$dom=new DomDocument();
$elementRoot=$dom->createElement('test');
$dom->appendChild($elementRoot);
echo $dom->saveXML();
exit();
?>

This works - sometimes. About half of the page refreshes I'm getting the following error:

Fatal error: Class 'DomDocument' not found in /www/eh6503-1/public_html/cms/testLibXML.php on line 3

I looked at my phpinfo(), and it says the following that are probably relevant:

PHP Version 5.1.6

Under "Configure Command": '--disable-dom'

Under "dom":
DOM/XML enabled
DOM/XML API Version 20031129
libxml Version 2.6.26

Under "libxml":
libxml
libXML support active
libXML Version 2.6.26
libXML streams enabled

Under "SimpleXML":
Simplexml support enabled
Revision $Revision: 1.151.2.22 $
Schema support enabled

Under "xml":
XML Support active
XML Namespace Support active
libxml2 Version 2.6.26

Under "xmlreader":
XMLReader enabled

Under "xsl":
XSL enabled
libxslt Version 1.1.17
libxslt compiled against libxml Version 2.6.26
EXSLT enabled
libexslt Version 1.1.17

==

I'm beginning to suspect that the code I'm using might be PHP 4 code. Please let me know if you have any suggestions on how I can get this working consistently, and advice as to which libraries you think are the forward-thinking way to go.

Thanks,

Erich

ariell
10-08-2008, 07:17 PM
Hi,

"--disable-dom" just means that the php processor was not compiled with NATIVE XML support. As long as phpinfo() shows what it shows, I'd feel fine.

(By the way, I like that "recovering...")

I prefer "SimpleXML", which is not an ultimate solution but enough "to code around", and: FAST. If you want the whole bunch (it is activated), keep in mind it comes at costs. As long as you handle relatively small files, it's OK. However, traversing a DOM tree of some n-thousand entries might start to ask serious questions to your host.

I recommend to simply read docs at php.net. If you have an ASP background, you'll quickly find out what suites your needs best.

Best from the south.

especht
10-09-2008, 09:01 AM
Thanks, Ariell for your reply. Does it make any sense that the script I posted is working some of the time? I've found other pieces of sample code that does the same thing - works some times, but errors out other times. I'm used to code doing the same thing every time it's called! Well, unless we're talking about Internet Explorer. ;)

ariell
10-09-2008, 02:08 PM
I'm used to code doing the same thing every time it's called! - Yes, me too!

Not quite sure, but I'd first of all avoid relative paths. The XML object model is not "finalized" yet in PHP (to put it like that). As for the script "length" you definitely don't need to fear any timeout trouble. So, what's else is that PHP might loose the handle "on the way" - that happens frequently, for instance, with ArrayObjects, which is why it's better to encapsulate all this stuff within own classes. Internally, traversing an XML tree and ArrayObjects are kind of sisters.

As I said: have a look at the SimpleXML API and WRAP it as needed. That way you can keep track on what is actually going on...

Best from the south.