General developer forum

XML parsing

 
Picture of Juliette Culver
XML parsing
 
Thought it was about time I said hello here! I'm a new Moodle developer at the Open University, but working on an externally-funded project rather than as part of the main Moodle team at the OU. The project is a joint one with the folk from LAMS and the work I'm doing is related to the management of groups.

I'm relatively new to PHP too and wanted to ask for some advice about parsing XML in PHP. I was looking at Dan Stowell's IMS Enterprise code and there's a comment there saying that there are differences between XML parsing in PHP4 and PHP5 so he uses regular expressions instead. Is this the best route to go or should I just use expat lib?




 
Average of ratings: Useful (1)
Picture of Dan Stowell
Re: XML parsing
 
Hi Juliette - welcome to the forums.

Actually, I didn't have any PHP 5 systems available at the time I wrote the script, but having discovered that the expat stuff seems to work the same in PHP 4 and PHP 5, I don't think there's actually any reason to avoid expat. I used expat for the WebCT import code and it works on either version.

(I was slightly misled by some articles suggesting that XML handling was "completely changed" in PHP 5.)

I'm happy to leave the IMS Enterprise code the way it is, since it works fast enough and in fact I find it easier to read, understand, and modify than expat-based code.
 
Average of ratings: Useful (1)
Picture of Juliette Culver
Re: XML parsing
 
I must admit and had a quick look and couldn't spot the difference between how expat worked for PHP4 and PHP5. I'm glad I wasn't being totally blind!
 
I think I'd actually find the code quicker and easier to write using regular expressions but suspect that most people would find it easier to read and maintain if I don't.

By the way, one of the things we're doing is an IMS Enterprises Services / IMS Enterprise type web services client in case you're interested. I presume you know about the MINTED project at Sussex too?
 
Average of ratings: Useful (1)
Picture of sam marshall
Re: XML parsing
Core developersParticularly helpful MoodlersPlugin developersTesters
The best way for general/full/complex use is to use the PHP DOM functions:

http://uk2.php.net/manual/en/ref.dom.php

These follow the W3C DOM so you will already be familiar with their use from other programming languages.

An alternate method, which is very easy if you only need simple usage, is to use the SimpleXML extension:

http://uk2.php.net/manual/en/ref.simplexml.php

However, both of these require PHP5, so if you are intending to target PHP4 (which the main Moodle community still supports for 1.6 and presumably (?) 1.7) then you'll need to choose another option. PHP4 did not have good XML support.

Personally if you're not intending to get your code into core I would just say it requires PHP5 and if somebody refuses to upgrade, that's their own problem for sticking in the dark ages/not changing hosting provider from one that's stuck in the dark ages. smile PHP is bad enough already without sticking to an antique implementation. But, if it goes in core you'll I guess need to support php4 (for the moment.

Incidentally I'm kind of vaguely interested (admittedly, only very vaguely) as to what you're doing... something to do with groups? Does this conflict with the general Moodle groups improvements that are due, or is it a part of that? I guess Jason (Cole) knows about your project so...

--sam
 
Average of ratings: Useful (1)
Picture of Juliette Culver
Re: XML parsing
 
We decided to stick to PHP4, but I'll need to check with the others how definite that decision is. I knew it'd rear its head at some point... (I'm not sure if there are plans to get the LAMS integration into core which this work is related to).

I'm aware that there are groups improvements in the pipeline and I'm going to try and make sure our stuff fits in with them in a reasonably sensible way as best I can, though I haven't had a proper look at the changes yet. But trying to coordinate our stuff too closely probably isn't going to be possible in terms of project timescales.

I'll drop you a line tomorrow - I'd be curious to know what you lot are working on over in LTS too!




 
Average of ratings: -
Picture of sam marshall
php5 in 1.7
Core developersParticularly helpful MoodlersPlugin developersTesters
Just to correct mys'elf here (well I did leave a question mark), Jason said earlier that he thinks Moodle 1.7 will require php5.

--sam
 
Average of ratings: -
Picture of Dan Stowell
Re: php5 in 1.7
 
Why?

The features in the 1.7 roadmap don't seem to require the new XML functions (possibly the repository API?). If there's a good reason for this then fine, but otherwise there's no point restricting the environments Moodle can be installed in (and plenty of 3rd-party hosts still provide PHP4, and there's no benefit to be had in slagging them off - not while there are still plenty of them around!).
 
Average of ratings: -
Tim at Lone Pine Koala Sanctuary
Re: php5 in 1.7
Core developersDocumentation writersParticularly helpful MoodlersPlugin developers
PHP 5 has lots of good stuff like better object orientation and exceptions. I don't think the XML stuff has any impact on the decision.
 
Average of ratings: -
Martin Dougiamas
php5 not required for 1.7
Core developersDocumentation writersMoodle HQPlugin developersTesters
Moodle 1.7 will NOT require PHP5. It has never been discussed that it would. PHP 4.3.0 is the current minimum and that'll be continuing until Moodle 2.0 at least.

DOM support is not included in PHP by default, so for most applications use lib/xmlize.php as Jun pointed out.
 
Average of ratings: -
Picture of sam marshall
Re: php5 not required for 1.7
Core developersParticularly helpful MoodlersPlugin developersTesters
Hah, that'll teach me to assume Jason is right...

(I of course think it would be better to require PHP5 immediately but. smile

--sam
 
Average of ratings: -
Picture of Jason Cole
Re: php5 not required for 1.7
 
I only said it MIGHT! I thought it had been discussed, but obviously I'm wrong. My apologies all around. blush
 
Average of ratings: -
Martin Dougiamas
Re: php5 not required for 1.7
Core developersDocumentation writersMoodle HQPlugin developersTesters
If I was in your position I'd be wanting exactly the same, but the fact is that many ISPs have not yet taken the plunge to it, so ..
 
Average of ratings: -
Picture of Juliette Culver
Re: php5 not required for 1.7
 
The lib/xmlize.php file actually contains a comment saying that it's a PHP5 version incidentally, though whether this matters in practice I don't know.

The lib/nusoap library for SOAP uses the expat lib by the looks of things, so I've done the same as we we're getting the XML we want to parse via SOAP, so if that's not working then it won't matter if our XML can't be parsed smile


 
Average of ratings: -
One poor developer...
Re: php5 not required for 1.7
Core developersDocumentation writersMoodle HQParticularly helpful MoodlersPlugin developersTesters
Hi,

lib/xmlize.php has been working pretty well since ages (it's used intensively in the backup/restore). And it works properly both under PHP4 and PHP5. Anyway some points are noticeable to be shared here, I think:

1.- The library uses one SAX (expat library) parser. Exactly like nusoap and others.

2.- Due to one Bug (or, at least, a different way of work) under PHP4 the behaviour of the parser isn't perfect (because PHP4 doesn't recognise the original encoding of the file), while it works properly after PHP5.0.2.
So, the "encoding" parameter in the xml_parser_create() function, has different meanings under PH4 and under PHP5.0.2 an later: Under PHP4 it's sets both the input and output encoding while under PHP5.0.2 it sets the output encoding only(because the input one is auto-detected).
And what about PHP5.0.0 and PHP5.0.1, well, better avoid to use them, they are different also. wink (you can find more info in the xml_parser_create () manual page.
So, after some tests, we proposed to Hans Anderson to add the "encoding" parameter to his library, defaulting to UTF-8. This will allow to control how it works under any PHP version, although we should work *always* with UTF-8 files (more with Moodle 1.6 upwards).

3.- xmlize library is perfect for small-medium XML documents. Don't use it for >1MB documents because it loads all the XML data in one BIG array, eating lots of RAM. It's really easy to use but only for reasonable sizes.

Ciao smile
 
Average of ratings: -
Ray Lawrence
Re: php5 not required for 1.7
Particularly helpful Moodlers
I was updating the instructions for installing Moodle on a windows PC using XAMPP at MoodleDocs when I noticed that Tim Hunt has crossed out another recent-ish addition and referenced this discussion.

In 1.5 devt when the Global Search block is added to a course this message is displayed if one is not running PHP 5:

"Sorry folks, PHP 5 is needed for the new search module."

Could the position with the PHP 5 requirement be clarified so that I can adapt this page in MoodleDocs to reflect the requirements accurately.

Cheers
 
Average of ratings: -
Picture of Michael Champanis
Re: php5 not required for 1.7
 
Global Search requires a PHP 5 installation (preferably 5.1.4) because it uses the Zend Framework Search module, which uses PHP 5-only features. Moodle does not require PHP 5, it is only this module that is disabled on non-PHP 5 installations.

Full-text searching can be done on PHP 4, using MySQL 4.1, but that's another discussion.
 
Average of ratings: -
Tim at Lone Pine Koala Sanctuary
Re: php5 not required for 1.7
Core developersDocumentation writersParticularly helpful MoodlersPlugin developers
Well, I would love to be able to assume PHP5 because of the better object oriented features and exception handling, to name but two things. However, Martin D has been insistant that we go on supporting PHP4 for the foreseeable future.

So, please Martin, can you clarify the situation with this. Thanks.
 
Average of ratings: -
Martin Dougiamas
Re: php5 not required for 1.7
Core developersDocumentation writersMoodle HQPlugin developersTesters
Sure.

1.7 - requires PHP4.3 or higher.
1.8 - requires PHP4.3 or higher.
2.0 - requires PHP5.1 or higher.

I'm still running into PHP4 everywhere.  Many distributions still ship with that (including Redhat Enterprise Linux 4 and Debian).  Accelerators like eaccelerator are only just now supporting PHP5 and it's still flaky.
 
Average of ratings: -
Picture of Dan Stowell
Re: php5 not required for 1.7
 
This should go in the Roadmap or Release Notes somewhere... I'll put a note about PHP 5 in the Roadmap entry for Moodle 2.

Mac OSX still ships with PHP4 too...
 
Average of ratings: -
Picture of Jun Yamog
Re: XML parsing
 
For simple parsing you can use "xmlize" function that is included in lib/xmlize.php  I was also thinking of using a php lib to parse my xml, someone pointed to me to use the existing one on moodle so reduce the dependency on external libraries.
 
Average of ratings: -