tags:

views:

1109

answers:

3

which one of the two is more spread? I want to read out the version number from http://freshmeat.net/projects-xml/mysql/mysql.xml?branch_id=46519 but I want to use the one which more people have.

If you know another way to get the latest stable version number from mysql please tell me ;)

+3  A: 

For this kind of task, reading the document into a DomDocument and using DomXPath is probably more suitable.

To answer your question, both libraries (as well as DomDocument + DomXPath) are standard outfit with PHP5, so they would be equally fine choices.

troelskn
I would specifically reccomend the LoadHTML file method as a good starting point as it was designed to work with this and wont be confused with sub tags.http://us3.php.net/manual/en/domdocument.loadhtml.php
preinheimer
Since this is XML and not HTML, loadXml would be more appropriate: http://docs.php.net/manual/en/domdocument.loadxml.php
troelskn
A: 

SimpleXML was introduced in PHP5 while XmlReader was only included by default in version 5.1, so the former is probably the best way to go:

$struct = simplexml_load_string($xml);
$version = (string)$struct->project->latest_release->latest_release_version;

However if you're not doing any other XML processing and want to maximise compatibility you could just regex the XML:

if(preg_match('/<latest_release_version>(.*)<\/latest_release_version>/', $xml, $matches)){
$version = $matches[1];
}

It's messier than processing the XML properly but is probably faster and supported by nearly all PHP installations.

Ciaran McNulty
+2  A: 

It has to be SimpleXML. It is enabled by default, is quicker to load XML documents than the Dom methods, has a smaller memory foot-print than the Dom methods, and has much simpler xpath methods than the Dom methods:

$xml = simplexml_load_file(
    'http://freshmeat.net/projects-xml/mysql/mysql.xml?branch_id=46519'
    );
$result = $xml->xpath('//latest_release/latest_release_version'); 
// or '//latest_release/*' if you'd rather loop through all release information.

while(list( , $node) = each($result))
    echo $node, "\n";
digitala